简体   繁体   English

在 MySQL 查询中插入来自多行的连接值作为新行

[英]Insert concatenated values from multiple rows as new row in MySQL query

I currently have a table with columns for "field_type" (username, city, state), "user_id", and "value".我目前有一个表,其中包含“field_type”(用户名、城市、州)、“user_id”和“value”列。 The user_id column obviously has lots of repeats. user_id 列显然有很多重复。 I'd like to merge the city and state data into a single "location" field_type value.我想将城市和 state 数据合并为一个“位置”字段类型值。 I need something that will:我需要一些东西:

for every integer in the user_id column: -check if there exist corresponding (not null) table rows for field_type "city" and "state" -if yes, insert a new row into the table with field_type "location" which concatenates the corresponding city and state values for that user_id

I haven't worked with MySQL much so I don't really know where to start.我没有与 MySQL 合作太多,所以我真的不知道从哪里开始。 I've tried to simplify the problem a bit - it's actually a somewhat more complicated wordpress table and I'm trying to reformat the data to be compatible with a new plugin, but this covers the basics of what has to happen so I should hopefully be able to extrapolate an actual solution from the answers.我试图将问题简化一点——它实际上是一个更复杂的 wordpress 表,我正在尝试重新格式化数据以与新插件兼容,但这涵盖了必须发生的事情的基础知识,所以我应该希望能够从答案中推断出实际的解决方案。 Thanks for any pointers!感谢您的任何指点!

Edit: Current structure looks like this: |-id (key)-|- field_type -|- user_id -|- value -| | 1 | username | 1 | Joe | | 2 | city | 1 | Albany | | 3 | state | 1 | NY | | 4 | username | 2 | Bob | | 5 | city | 2 | Toledo | | 6 | state | 2 | OH |编辑:当前结构如下所示: |-id (key)-|- field_type -|- user_id -|- value -| | 1 | username | 1 | Joe | | 2 | city | 1 | Albany | | 3 | state | 1 | NY | | 4 | username | 2 | Bob | | 5 | city | 2 | Toledo | | 6 | state | 2 | OH | |-id (key)-|- field_type -|- user_id -|- value -| | 1 | username | 1 | Joe | | 2 | city | 1 | Albany | | 3 | state | 1 | NY | | 4 | username | 2 | Bob | | 5 | city | 2 | Toledo | | 6 | state | 2 | OH |

And I would like to get something like this: |-id (key)-|- field_type -|- user_id -|- value ---------| | 1 | username | 1 | Joe | | 2 | city | 1 | Albany | | 3 | state | 1 | NY | | 4 | username | 2 | Bob | | 5 | city | 2 | Toledo | | 6 | state | 2 | OH | | 7 | location | 1 | Albany, NY | | 8 | location | 2 | Toledo, OH |我想得到这样的东西: |-id (key)-|- field_type -|- user_id -|- value ---------| | 1 | username | 1 | Joe | | 2 | city | 1 | Albany | | 3 | state | 1 | NY | | 4 | username | 2 | Bob | | 5 | city | 2 | Toledo | | 6 | state | 2 | OH | | 7 | location | 1 | Albany, NY | | 8 | location | 2 | Toledo, OH | |-id (key)-|- field_type -|- user_id -|- value ---------| | 1 | username | 1 | Joe | | 2 | city | 1 | Albany | | 3 | state | 1 | NY | | 4 | username | 2 | Bob | | 5 | city | 2 | Toledo | | 6 | state | 2 | OH | | 7 | location | 1 | Albany, NY | | 8 | location | 2 | Toledo, OH |

Duplicate user_id values are how it's supposed to work, so they don't need to be removed.重复的 user_id 值是它应该如何工作的,所以它们不需要被删除。

You could use an INSERT... SELECT query to do this:您可以使用INSERT... SELECT查询来执行此操作:

INSERT INTO yourtable
SELECT 'location' AS field_type, t1.user_id, CONCAT(t1.value, ' ', t2.value) AS value
FROM yourtable t1
JOIN yourtable t2 ON t2.user_id = t1.user_id AND t1.field_type = 'city' AND t2.field_type = 'state'
WHERE t1.value IS NOT NULL AND t2.value IS NOT NULL

Demo on dbfiddle dbfiddle 上的演示

Since you are mentioned that there are duplicates with the same user_id.由于您提到存在具有相同 user_id 的重复项。 I don't think inserting a new row will be a good idea.我认为插入新行不是一个好主意。 So have written an update query to update the existing data.所以写了一个更新查询来更新现有数据。 You can later cleanup the duplciates.您可以稍后清理重复项。

UPDATE your_table SET locations = CONCAT_WS('-', state, city) where city is not null or state is not null

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

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