简体   繁体   English

如何设置列的值是使用同一表的列之一的SQL查询的结果

[英]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 具有列idNameinstitution

Institution is a sting showing institution name. 机构是显示机构名称的字符串。

Table 2: Institutions. 表2:机构。

Has columns id , institution , count 有列idinstitutioncount

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.

相关问题 sql连接中存在相同名称的两列时,如何从一个表列获取值 - How to get value from one table column when two columns of the same name exist in an sql join 如何使用单个查询执行多个算术运算并将答案存储在同一表的两个不同列中 - How can I do more than one arithmetic operation using a single query and store the answer in two different columns on the same table 如何在一个表中查询用户值到列标题并在标题下获取值? - How do I query a user value in one table to a column header and get value under header? 如何使用一个SQL查询从两个表中获得结果? - how do I get a result from two tables using one SQL Query? 如何将两个mysql列加在一起,然后将结果更新为相同的列之一? - How do I add two mysql columns together and then update the result to one of the same columns? 在mySQL中,如何在一个查询中获取并设置值? - In mySQL, how do I get and set a value in one query? 如何从 php 中的 SQL 查询中获取一个值? - How do I get one value from an SQL query in php? 如何为另一个列中具有相同值的每个结果将时间戳记值加一 - How can I increase a timestamp value by one for each result with same value in another column 如何分隔sql查询的结果? - How do I separate the result of an sql query? 如何使用同一表格查询共同的朋友? - How do i query for mutual friends using the same table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM