[英]Add multiple rows in just one row from a single table
How can I join multiple rows in just one single row through mysql? 我怎样才能通过mysql在单行中加入多行?
Example : 范例:
Student Table 学生桌
Sno.| Name | Subjects
1. | ABC | English
2. | ABC | Mathematics
3. | ABC | Science
4. | FMC | French
5. | ABC | Russian
6. | JBC | French
Now I want it in this format 现在我想要这种格式
Sno.| Name | Sub1 | Sub2 | Sub3 | Sub4 |
1. | ABC | Eng | Maths| Science| Russian
2. | FMC | French| Null| Null | Null
3. | JBC | French| Null | Null | Null
I am not sure how to actually do it? 我不确定该怎么做? And shall I create a view or a table? 我应该创建视图还是表格?
I guess a view will be fine. 我想一个观点会很好。
I agree with the other answers, that GROUP_CONCAT
along with PHP to split the comma separated values is probably the best approach, however if for any other reason you needed the output you suggested via Pure SQL I would suggest one of the following appoaches. 我同意其他答案, GROUP_CONCAT
与PHP一起分割逗号分隔的值可能是最好的方法,但是,如果出于任何其他原因需要通过Pure SQL建议的输出,则建议使用以下方法之一。
SELECT t1.Name,
MIN(t1.Subject) AS Sub1,
MIN(t2.Subject) AS Sub2,
MIN(t3.Subject) AS Sub3,
MIN(t4.Subject) AS Sub4
FROM Students t1
LEFT JOIN Students T2
ON t1.Name = t2.Name
AND t2.Subject > t1.Subject
LEFT JOIN Students T3
ON t2.Name = t3.Name
AND t3.Subject > t2.Subject
LEFT JOIN Students T4
ON t3.Name = t4.Name
AND t4.Subject > t3.Subject
GROUP BY t1.Name;
2. Using a ROW_NUMBER Type function to aggregate 2.使用ROW_NUMBER类型函数进行汇总
SELECT Name,
MAX(IF(RowNum = 1,Subject, NULL)) AS Sub1,
MAX(IF(RowNum = 2,Subject, NULL)) AS Sub2,
MAX(IF(RowNum = 3,Subject, NULL)) AS Sub3,
MAX(IF(RowNum = 4,Subject, NULL)) AS Sub4
FROM ( SELECT Name,
Subject,
@r:= IF(@Name = Name, @r + 1, 1) AS RowNum,
@Name:= Name AS Name2
FROM Students,
(SELECT @Name:='') n,
(SELECT @r:= 0) r
ORDER BY Name, Sno
) t
GROUP BY Name
Using below query, get the Name and his/ her subjects. 使用以下查询,获取姓名和他/她的主题。
SELECT Name, GROUP_CONCAT(Subjects) AS List
FROM myTable
GROUP BY Name
Then in PHP, you can use implode
function for printing subjects. 然后,在PHP中,可以使用implode
函数打印主题。
Hope this helps. 希望这可以帮助。
Try using GROUP BY and GROUP_CONCAT : 尝试使用GROUP BY和GROUP_CONCAT :
SELECT Name, GROUP_CONCAT(Subjects) AS Subjects_list
FROM students_table
GROUP BY Name
Then use PHP function explode while fetching records to get the different values stored in Subjects_list column. 然后在获取记录时使用PHP函数explode来获取存储在Subjects_list列中的不同值。
It ain't pretty! 不好看!
To use the above solutions, you could do: 要使用上述解决方案,您可以执行以下操作:
SELECT
Sno,
Name,
NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(all_subjects, ',', 1), ',', -1), '') AS Sub1,
NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(all_subjects, ',', 2), ',', -1), '') AS Sub2,
NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(all_subjects, ',', 3), ',', -1), '') AS Sub3,
NULLIF(SUBSTRING_INDEX(SUBSTRING_INDEX(all_subjects, ',', 4), ',', -1), '') AS Sub4
FROM (
SELECT Sno, Name, CONCAT(GROUP_CONCAT(Subjects ORDER BY Sno),',,,') AS all_subjects FROM table GROUP BY name
) inner_sel
;
Your requests leads to the assumption there can be never more than 4 subjects. 您的要求导致一个假设,即不得超过4个科目。 You also require NULL
s where there is no mention of four subjects. 您还需要NULL
,而没有提到四个主题。
There kind of requests lead to a forced SQL code, one which is typically what SQL is intended for. 有一种请求会导致强制执行SQL代码,这通常是SQL的目的。
To explain the above: we use GROUP_CONCAT
, then break it again to pieces. 为了解释以上内容:我们使用GROUP_CONCAT
,然后再将其分解成碎片。 Since there could be fewer than 4 elements, we pad with commas ( ',,,'
). 由于元素可能少于4个 ,因此我们用逗号( ',,,'
)填充。 We then break each piece according to its place within the concatenated string, and return with NULL
if it's empty. 然后,我们根据每个字符串在串联字符串中的位置将其断开,如果为空,则返回NULL
。
Can't say this is one of my better answers in terms of prettiness. 就美而言,不能说这是我更好的答案之一。 I hope you find it useful. 希望对你有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.