Home > Backend Development > PHP Tutorial > 这种查询要求的SQL语句应该怎么写

这种查询要求的SQL语句应该怎么写

WBOY
Release: 2016-06-23 13:35:26
Original
1082 people have browsed it

表如下

A               B            
中国          1
中国          2
中国          3
美国          4
美国          5
英国          6
英国          7
.
.
.

-------------------最终查询出来结果要求是下面这样的
中国             1,2,3
美国             4,5
英国             6,7

我之前过直接用GROUP BY ,但A字段是能分组出来,但B字段的集合怎么出现呀?难道要再嵌一个SQL???


回复讨论(解决方案)

select A, group_concat(B) as B from 表 group by A
Copy after login
Copy after login

select A, group_concat(B) as B from 表 group by A
Copy after login
Copy after login



果然好用,但有点新问题,字段里同一组有相同值的B,如
A               B            
中国          1
中国          2
中国          1
中国          2
中国          1
-------------------------能显示成吗?我先研究一下上面的group_concat,说不定自己就能解决了,哈哈------------------------
中国          1,2

用了[DISTINCT],好用,谢谢楼上,结分

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template