MySQL:UPDATE表与另一个表中的COUNT? [英] MySQL: UPDATE table with COUNT from another table?

查看:243
本文介绍了MySQL:UPDATE表与另一个表中的COUNT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为这很简单,但是我无法理解...

I thought this would be simple but I can't get my head around it...

我有一个表tbl1,它具有列idotherstuffnum.

I have one table tbl1 and it has columns id,otherstuff,num.

我有另一个表tbl2,它具有列idinfo.

I have another table tbl2 and it has columns id,info.

我要使tbl1num列等于tbl2中具有相同id的行数.有点像这样:

What I want to is make the num column of tbl1 equal to the number of rows with the same id in tbl2. Kind of like this:

UPDATE tbl1 SET num =
(SELECT COUNT(*) FROM tbl2 WHERE id=tbl1.id)

有什么想法吗?

推荐答案

如果您的num列是有效的数字类型,则您的查询应按原样工作:

If your num column is a valid numeric type your query should work as is:

UPDATE tbl1 SET num = (SELECT COUNT(*) FROM tbl2 WHERE id=tbl1.id)

这篇关于MySQL:UPDATE表与另一个表中的COUNT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆