简体   繁体   English

如何使用mysql将一个表中的所有列集成到另一个表的一列中

[英]how to integrate all columns from one table into one column of another table using mysql

table 1: employee 表1:员工

name| mobile| location
alex| 123   | australia
john| 456   | paris
kohl| 678   | australia

table 2:employment 表2:就业

id|location |data
1 |australia|[{"name":"alex","mobile":"123"},{"name":"kohl","mobile":"678"}]
2 |paris    |[{"name":"john","mobile":"456"}]

i have two tables named "employee" and "employment". 我有两张名为“员工”和“就业”的表。 How can i get all the column values of employee table into one column of employment table as shown in table 2. I am new to SQL querying. 如何将employee表的所有列值都放入一个就业表的列中,如表2所示。我是SQL查询的新手。 I honestly don't have any idea on how to proceed. 老实说,我对如何进行一无所知。 Any pointers and suggestions are appreciated. 任何指针和建议都表示赞赏。

You can use the following solution using CONCAT and GROUP_CONCAT to get this result: 您可以使用以下解决方案使用CONCATGROUP_CONCAT来获得此结果:

SELECT location, CONCAT("[", GROUP_CONCAT(CONCAT('{"name":"', name, '","mobile":"', mobile, '"}')), "]") AS data
FROM employee
GROUP BY location

demo: http://sqlfiddle.com/#!9/ab25ee/2/0 演示: http //sqlfiddle.com/#!9 / abs25 / 2/0

To JOIN the employee table with the employment you can use the following solution: 要使用employmentJOIN employee表,您可以使用以下解决方案:

SELECT employment.id, employment.location, CONCAT("[", GROUP_CONCAT(CONCAT('{"name":"', name, '","mobile":"', mobile, '"}')), "]") AS data 
FROM employment INNER JOIN employee ON employment.location = employee.location 
GROUP BY id, location

demo: http://sqlfiddle.com/#!9/12f9c9/1/0 演示: http //sqlfiddle.com/#!9/12f9c9/1/0

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

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