基于上一次查询的结果更新表

kqhtkvqz  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(359)

如何根据上一个查询的结果更新表?
最初的查询(感谢gmb)可以在address(users表)中找到与address(address\u effect表)匹配的任何项。
从这个查询的结果中,我想在address\u effect表中找到address的计数,并将其添加到“users”表的一个新列中。例如,john doe在address列中与idaho和usa匹配,因此在count列中显示count为'2'。
仅供参考,我正在用xampp(使用mariadb)在本地系统上测试这个。

用户表

+--------+-------------+---------------+--------------------------+--------+
|    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         |
+---------+----------------+
|idaho    |potato, tater   |
+--------------------------+
|canada   |cold, tundra    |
+--------------------------+
|fremont  | crowded        |
+--------------------------+
|peru     |alpaca          |
+--------------------------+
|atlanta  |peach, cnn      |
+--------------------------+
|usa      |big, hard       |
+--------+-----------------+
aij0ehis

aij0ehis1#

使用返回匹配数的相关子查询:

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

请看演示。
结果:

> 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
yruzcnhs

yruzcnhs2#

注意:我在mysql中检查了它,但在mariadb中没有。
可以使用带有内部联接的update语句更新users表的count列。然后您可以使用一个查询,该查询将原始查询修改为使用“groupby”。

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;

相关问题