简体   繁体   English

根据先前查询的结果更新表

[英]Updating table based on the results of previous query

How can I update the table based on the results of the previous query?如何根据上一个查询的结果更新表?

The original query (big thanks to GMB ) can find any items in address (users table) that have a match in address (address_effect table). 原始查询(非常感谢GMB )可以在地址(用户表)中找到与地址(地址效果表)匹配的任何项目。

From the result of this query, I want to find the count of address in the address_effect table and add it into a new column in the table “users”.从这个查询的结果中,我想在 address_effect 表中找到地址的计数,并将其添加到表“users”中的一个新列中。 For example, john doe has a match with idaho and usa in the address column so it'll show a count of '2' in the count column.例如,john doe 在地址列中与 idaho 和 usa 匹配,因此它将在计数列中显示计数“2”。

Fyi, I'm testing this on my local system with XAMPP (using MariaDB).仅供参考,我正在使用 XAMPP(使用 MariaDB)在我的本地系统上对此进行测试。

user table用户表

+--------+-------------+---------------+--------------------------+--------+
|    ID  |  firstname  |  lastname     |    address               |  count |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     1  |    john     |    doe        |james street, idaho, usa  |        |                    
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     2  |    cindy    |   smith       |rollingwood av,lyn, canada|        |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     3  |    rita     |   chatsworth  |arajo ct, alameda, cali   |        |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     4  |    randy    |   plies       |smith spring, lima, peru  |        |                       
|        |             |               |                          |        |
+--------------------------------------------------------------------------+
|     5  |    Matt     |   gwalio      |park lane, atlanta, usa   |        |
|        |             |               |                          |        |
+--------------------------------------------------------------------------+

address_effect table地址效果表

+---------+----------------+
|address   |effect         |
+---------+----------------+
|idaho    |potato, tater   |
+--------------------------+
|canada   |cold, tundra    |
+--------------------------+
|fremont  | crowded        |
+--------------------------+
|peru     |alpaca          |
+--------------------------+
|atlanta  |peach, cnn      |
+--------------------------+
|usa      |big, hard       |
+--------+-----------------+

Notice: I checked it in MySQL, but not in MariaDB.注意:我在 MySQL 中检查了它,但在 MariaDB 中没有。

The count column of users table may be able to be updated using UPDATE statement with INNER JOIN.用户表的计数列可以使用带有 INNER JOIN 的 UPDATE 语句进行更新。 Then you can use a query that modifies the original query to use "GROUP BY".然后,您可以使用修改原始查询的查询以使用“GROUP BY”。

UPDATE users AS u
INNER JOIN
(
  -- your original query modified
  SELECT u.ID AS ID, count(u.ID) AS count
  FROM users u
  INNER JOIN address_effect a 
      ON FIND_IN_SET(a.address, REPLACE(u.address, ', ', ','))
  GROUP BY u.ID
) AS c ON u.ID=c.ID
SET u.count=c.count;

Use a correlated subquery which returns the number of matches:使用返回匹配数的相关子查询:

UPDATE user u
SET u.count = (
  SELECT COUNT(*)
  FROM address_effect a
  WHERE FIND_IN_SET(a.address, REPLACE(u.address, ', ', ','))
)

See the demo .演示
Results:结果:

> ID | firstname | lastname   | address                    | count
> -: | :-------- | :--------- | :------------------------- | ----:
>  1 | john      | doe        | james street, idaho, usa   |     2
>  2 | cindy     | smith      | rollingwood av,lyn, canada |     1
>  3 | rita      | chatsworth | arajo ct, alameda, cali    |     0
>  4 | randy     | plies      | smith spring, lima, peru   |     1
>  5 | Matt      | gwalio     | park lane, atlanta, usa    |     2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM