Updating table based on the results of previous query

老子叫甜甜 提交于 2020-07-20 03:47:30

问题


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).

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”. 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.

Fyi, I'm testing this on my local system with XAMPP (using 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       |
+--------+-----------------+

回答1:


Notice: I checked it in MySQL, but not in MariaDB.

The count column of users table may be able to be updated using UPDATE statement with INNER JOIN. Then you can use a query that modifies the original query to use "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;



回答2:


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


来源:https://stackoverflow.com/questions/62964855/updating-table-based-on-the-results-of-previous-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!