[英]How do I set the value of a column be a result of a SQL query using one of the columns of the same table
I have two tables 我有两张桌子
Table 1 : Users 表1:用户
Has columns id
, Name
, institution
具有列
id
, Name
, institution
Institution is a sting showing institution name. 机构是显示机构名称的字符串。
Table 2: Institutions. 表2:机构。
Has columns id
, institution
, count
有列
id
, institution
, count
I need the count
column to be recording the number of rows in table 1 where institution
is same as the institution
in each row of Table 2. This should happen for all the rows of Table 2. 我需要
count
列来记录表1中的行数,其中institution
与表2的每一行中的institution
相同。这应该发生在表2的所有行中。
Please help me get the right MySQL queries or table relations to achieve this. 请帮助我获得正确的MySQL查询或表关系以实现此目的。
Join
both the tables on instutions
and get the count like below Join
两个表连接到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
this question doesn't have a lot of detail but from what I understand you just want to do this. 这个问题没有很多细节,但是据我了解,您只是想这样做。
UPDATE TABLE institutions,
(SELECT
i.id,
COUNT(u.id) as user_count
FROM Users u
JOIN Institutions i on i.institution = u.institution
GROUP BY i.institution) as temp
SET institutions.count = temp.user_count
WHERE temp.id = institutions.id;
I'd do something like this: 我会做这样的事情:
UPDATE institutions i
LEFT
JOIN ( SELECT u.institution
, COUNT(1) AS user_cnt
FROM users u
GROUP BY u.institution
) c
ON c.institution = i.institution
SET i.count = IFNULL(c.user_cnt,0)
The inline view (aliased as c) gets a count of users for each "institution". 内联视图(别名为c)获取每个“机构”的用户数。 This result is outer joined to institutions.
这个结果是外部加入机构的。 (We reference the result from that inline view query like it was a table; MySQL actually refers to the inline view as a "derived table".
(我们引用该内联视图查询的结果就像是一个表; MySQL实际上将内联视图称为“派生表”。
We assign the value derived user_cnt
value to the count
column, and substituting 0 for NULL (which will happen if there are no users in a given "institution".) 我们将值派生的
user_cnt
值分配给count
列,并用0代替NULL(如果在给定的“机构”中没有用户,则会发生这种情况。)
To see this in action BEFORE you run the update, you can run a similar SELECT (remove the SET clause and change the UPDATE
keyword to be SELECT <expression_list> FROM
, optionally add an ORDER BY
clause. 要在运行更新之前查看此操作,可以运行类似的SELECT(删除SET子句,并将
UPDATE
关键字更改为SELECT <expression_list> FROM
,还可以选择添加ORDER BY
子句。
SELECT i.institution
, i.count AS old_count
, IFNULL(c.user_cnt,0) AS new_count
FROM institutions i
LEFT
JOIN ( SELECT u.institution
, COUNT(1) AS user_cnt
FROM users u
GROUP BY u.institution
) c
ON c.institution = i.institution
ORDER BY i.institution
As another possible approach to achieve the same result, but with (likely) less efficiency (if this is actually valid), would be to use a correlated subquery to return the value: 作为获得相同结果但效率(可能)较低(如果这实际上是有效的)的另一种可行方法,将是使用相关子查询来返回值:
UPDATE institutions i
SET i.count = ( SELECT COUNT(1)
FROM users u
WHERE u.institution = i.institution
)
I know that approach "works" in a SELECT statement. 我知道这种方法在SELECT语句中“有效”。 I expect it to work in an UPDATE statement, but I haven't tested that.
我希望它可以在UPDATE语句中工作,但我尚未对其进行测试。 Equivalent SELECT would be:
等效的SELECT将是:
SELECT i.institution
, i.count AS old_count
, ( SELECT COUNT(1)
FROM users u
WHERE u.institution = i.institution
) AS new_count
FROM institutions i
Absent any compelling reason to use a correlated subquery, I'd avoid that and go with the JOIN operation instead. 缺少使用相关子查询的任何令人信服的理由,我会避免这种情况,而改为使用JOIN操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.