我有表
Table1 user_id, date
Table2 user_id, status
我需要类似的东西
UPDATE Table2
SET status = 2
WHERE user_id in ( {SELECT user_id FROM Table1 WHERE date > 0} )
换句话说,我需要查看 table1 中的日期是否大于 0000-00-00,然后获取符合此条件的人的 user_id,并在 table2 中使用它们将他们的状态设置为 2。 问题是我需要为多个用户执行此操作,因此请求内部请求对我不起作用,它仅在结果为一行时才有效。
最佳答案
我认为你写的应该有用,但是花括号是怎么回事?只需将它们移除即可:
UPDATE Table2
SET status = 2
WHERE user_id in (SELECT user_id FROM Table1 WHERE date > 0)
根据MySQL documentation , IN 应该使用子查询。
The ANY keyword, which must follow a comparison operator, means “return TRUE if the comparison is TRUE for ANY of the values in the column that the subquery returns.”
When used with a subquery, the word IN is an alias for = ANY.
关于来自两个相互交互的表的 MySQL 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7828667/