麻烦给改下下面错误的sql语句

qqwx_1986 2009-07-09 07:55:21
要求更改查询到的第一个满足tag=0条件的改为tag=1
就是这个意思,下面是错的
update table1 set tag=1 where id=(select id from table1 where tag=0 limit 0,1);
...全文
61 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqwx_1986 2009-07-09
  • 打赏
  • 举报
回复
谢了,还是慢慢消化得了,呵呵
ACMAIN_CHM 2009-07-09
  • 打赏
  • 举报
回复

select id from t_qqwx_1986 where tag=0 limit 0,1

这一句明白吗?

(select id from t_qqwx_1986 where tag=0 limit 0,1) b 只是定义了个子查询,
qqwx_1986 2009-07-09
  • 打赏
  • 举报
回复
谢了
(select id from t_qqwx_1986 where tag=0 limit 0,1) b
这个地方有点不明白什么意思,能说明下最好了
ACMAIN_CHM 2009-07-09
  • 打赏
  • 举报
回复

少写了个 from
update t_qqwx_1986 a, (select id from t_qqwx_1986 where tag=0 limit 0,1) b
set a.tag=1
where a.id=b.id


测试如下.

mysql> select * from t_qqwx_1986;
+----+------+
| id | tag |
+----+------+
| 1 | 1 |
| 2 | 0 |
| 3 | 0 |
| 4 | 1 |
+----+------+
4 rows in set (0.00 sec)

mysql> update t_qqwx_1986 a, (select id from t_qqwx_1986 where tag=0 limit 0,1) b
-> set a.tag=1
-> where a.id=b.id;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from t_qqwx_1986;
+----+------+
| id | tag |
+----+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 0 |
| 4 | 1 |
+----+------+
4 rows in set (0.00 sec)

mysql>
qqwx_1986 2009-07-09
  • 打赏
  • 举报
回复
非常感谢两位,一楼的不行
二楼的没怎么看懂,试了下也不行
ACMAIN_CHM 2009-07-09
  • 打赏
  • 举报
回复

update table1 a, (select id table1 where tag=0 limit 0,1) b
set a.tag=1
where a.id=b.id


qq9791541 2009-07-09
  • 打赏
  • 举报
回复
update table1 set tag=1 where id in(select id from table1 where tag=0 limit 0,1)

56,940

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧