gpt4 book ai didi

php - 如何将列的值设置为使用同一表的列之一进行 SQL 查询的结果

转载 作者:行者123 更新时间:2023-11-29 12:52:39 25 4
gpt4 key购买 nike

我有两张 table

表 1:用户

有列idNameinstitution

Institution 是一个显示机构名称的字符串。

表 2:机构。

有列idinstitutioncount

我需要 count 列记录表 1 中的行数,其中 institution 与每行中的 institution 相同表 2。表 2 的所有行都应该发生这种情况。

请帮助我获得正确的 MySQL 查询或表关系来实现此目的。

最佳答案

加入instutions上的两个表并获得如下所示的计数

select i.institution,
X.ins_count
from Institutions i
left join
(
select institution,
count(*) as ins_count
from Users
group by institution
) X on i.institutions = X.institutions

关于php - 如何将列的值设置为使用同一表的列之一进行 SQL 查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24498593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com