Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我以为这很简单,但我无法理解它......
我有一张桌子,tbl1它有列id,,,。otherstuffnum
tbl1
id
otherstuff
num
我有另一个表tbl2,它有列id,info。
tbl2
info
我想要的是使 的num列tbl1等于具有相同idin的行数tbl2。有点像这样:
UPDATE tbl1 SET num = (SELECT COUNT(*) FROM tbl2 WHERE id=tbl1.id)
有任何想法吗?
如果您的 num 列是有效的数字类型,则您的查询应该按原样工作:
UPDATE tbl1, (select id, count(*) as idCount from tbl2 group by id) as t2 SET tbl1.num = t2.idCount WHERE tbl1.id = t2.id;