我对这个SQL语句有一个问题,它打印带有重复行的结果,这在我的SQL语句中很烦人:
SELECT
week_day,
exam_date ,
s.name ,
l.lgna_name
,f.first_name
FROM
fci_exam_time_table_line,
fci_subject s ,
fci_staff f,
lgna_lgna l
WHERE
exam_id =%d
GROUP BY
week_day,
exam_date ,
s.name ,
l.lgna_name ,
f.first_name
ORDER BY
exam_date
并打印如下
week_day| exam_date |name|lgna_name|first_name
"monday";"2015-04-13";"12"; "12" ;"12"
"monday";"2015-04-13";"12"; "12" ;"323"
"monday";"2015-04-13";"12"; "232" ;"12"
"monday";"2015-04-13";"12"; "232" ;"323"
"monday";"2015-04-14";"12"; "12" ;"12"
"monday";"2015-04-14";"12"; "12" ;"323"
"monday";"2015-04-14";"12"; "232" ;"12"
"monday";"2015-04-14";"12"; "232" ;"323"
我希望它打印出来像:
week_day| exam_date |name|lgna_name|first_name
"monday";"2015-04-13";"12"; "12" ;"12,323"
"monday";"2015-04-13";"12"; "232";"12,323"
"monday";"2015-04-14";"12"; "12" ;"12,323"
"monday";"2015-04-14";"12"; "232" ;"12,323"
答案 0 :(得分:0)
使用Group_Concat
功能,然后从f.first_name
Group by
SELECT week_day,
exam_date,
s.name,
l.lgna_name,
Group_conact(f.first_name, ',')
FROM fci_exam_time_table_line,
fci_subject s,
fci_staff f,
lgna_lgna l
WHERE exam_id =% d
GROUP BY week_day,
exam_date,
s.name,
l.lgna_name
ORDER BY exam_date
Postgres
使用array_to_string
和array_agg
。有关解释,请查看here
select ...
array_to_string(array_agg(f.first_name), ',')
FROM fci_exam_time_table_line,
fci_subject s,
fci_staff f,
lgna_lgna l
WHERE exam_id =% d
GROUP BY week_day,
exam_date,
s.name,
l.lgna_name
ORDER BY exam_date