通过外键将值列表放入一列
Get list of values into one column through foreign key
我有两个表 "sample" 和 "processes",我想在一个列中获取给定样本的所有过程。
Table "sample" 示例:
----------------------------------------
| Sample Table |
| |
|id | timestamp | other columns... |
--------------------------------------
|1 | 24/04/1994 | ... |
|2 | 25/04/1994 | ... |
|... |... | .... |
----------------------------------------
Table "processes" 示例:
----------------------------------------
| Processes Table |
| |
|id | sample_id | process_name |
--------------------------------------
|1 | 1 | facebook |
|2 | 1 | tinder |
|3 | 1 | clash royale |
|4 | 2 | uno |
|5 | 2 | whatsapp |
|... |... | .... |
----------------------------------------
结果:
------------------------------------------------------------
| Result Table |
| |
|sample_id | timestamp | processes |
-----------------------------------------------------------
|1 | 24/04/1994 | [facebook, tinder, clash royale] |
|2 | 25/04/1994 | [uno, whatsapp] |
------------------------------------------------------------
可以通过SQL查询吗?如何?
P.S: 我知道我可以得到给定样本的所有进程:
SELECT sample_id, timestamp, process_name
FROM sample, processes
WHERE sample.id = processes.sample_id
但这给了我太多不必要的行
您可以尝试使用 GROUP_CONCAT
和 CONCAT
功能。
架构(MySQL v5.7)
CREATE TABLE sample(
id INT,
timestamp DATE
);
INSERT INTO sample VALUES (1,'1994/04/24');
INSERT INTO sample VALUES (2,'1994/04/25');
CREATE TABLE processes(
id INT,
sample_id INT,
process_name VARCHAR(50)
);
INSERT INTO processes VALUES (1 ,1,'facebook');
INSERT INTO processes VALUES (2 ,1,'tinder');
INSERT INTO processes VALUES (3 ,1,'clash');
INSERT INTO processes VALUES (4 ,2,'uno');
INSERT INTO processes VALUES (5 ,2,'whatsapp');
查询#1
SELECT sample_id, timestamp, CONCAT('[',GROUP_CONCAT(process_name),']') processes
FROM sample JOIN processes ON sample.id = processes.sample_id
GROUP BY sample_id, timestamp;
| sample_id | timestamp | processes |
| --------- | ---------- | ----------------------- |
| 1 | 1994-04-24 | [tinder,clash,facebook] |
| 2 | 1994-04-25 | [uno,whatsapp] |
注意
我会使用 JOIN
而不是 ,
因为 JOIN
强调连接两个表的关系。
我有两个表 "sample" 和 "processes",我想在一个列中获取给定样本的所有过程。
Table "sample" 示例:
----------------------------------------
| Sample Table |
| |
|id | timestamp | other columns... |
--------------------------------------
|1 | 24/04/1994 | ... |
|2 | 25/04/1994 | ... |
|... |... | .... |
----------------------------------------
Table "processes" 示例:
----------------------------------------
| Processes Table |
| |
|id | sample_id | process_name |
--------------------------------------
|1 | 1 | facebook |
|2 | 1 | tinder |
|3 | 1 | clash royale |
|4 | 2 | uno |
|5 | 2 | whatsapp |
|... |... | .... |
----------------------------------------
结果:
------------------------------------------------------------
| Result Table |
| |
|sample_id | timestamp | processes |
-----------------------------------------------------------
|1 | 24/04/1994 | [facebook, tinder, clash royale] |
|2 | 25/04/1994 | [uno, whatsapp] |
------------------------------------------------------------
可以通过SQL查询吗?如何?
P.S: 我知道我可以得到给定样本的所有进程:
SELECT sample_id, timestamp, process_name
FROM sample, processes
WHERE sample.id = processes.sample_id
但这给了我太多不必要的行
您可以尝试使用 GROUP_CONCAT
和 CONCAT
功能。
架构(MySQL v5.7)
CREATE TABLE sample(
id INT,
timestamp DATE
);
INSERT INTO sample VALUES (1,'1994/04/24');
INSERT INTO sample VALUES (2,'1994/04/25');
CREATE TABLE processes(
id INT,
sample_id INT,
process_name VARCHAR(50)
);
INSERT INTO processes VALUES (1 ,1,'facebook');
INSERT INTO processes VALUES (2 ,1,'tinder');
INSERT INTO processes VALUES (3 ,1,'clash');
INSERT INTO processes VALUES (4 ,2,'uno');
INSERT INTO processes VALUES (5 ,2,'whatsapp');
查询#1
SELECT sample_id, timestamp, CONCAT('[',GROUP_CONCAT(process_name),']') processes
FROM sample JOIN processes ON sample.id = processes.sample_id
GROUP BY sample_id, timestamp;
| sample_id | timestamp | processes |
| --------- | ---------- | ----------------------- |
| 1 | 1994-04-24 | [tinder,clash,facebook] |
| 2 | 1994-04-25 | [uno,whatsapp] |
注意
我会使用 JOIN
而不是 ,
因为 JOIN
强调连接两个表的关系。