group_concat,substring_index

2018-08-14  本文已影响34人  好小葱1

示例1

select year,month GROUP_CONCAT(volumn) from magazine group by year,month order by year desc, month desc;  
year    month   GROUP_CONCAT(volumn)
2010    12  1,2,3,4
2010    11  1,2,3,4,5

示例2

SELECT 
ticket_id, 
GROUP_CONCAT(DISTINCT odd_id ORDER BY odd_id ASC) as oddsconcat 
FROM ticket_odds 
GROUP BY ticket_id 
HAVING oddsconcat = .....

示例3

在一个表ta中数据为:

+----+------+
| id | name |
+----+------+
| 1 | a    |
| 1 | b    |
| 1 | c    |
| 1 | d    |
| 2 | a    |
| 2 | b    |
| 2 | c    |
| 3 | d    |
+----+------+

从里面取数据要求完成如下效果:

+----+--------+
| id | name    |
+----+--------+
| 1 | a,b,c,d |
| 2 | a,b,c    |
| 3 | d          |
+----+--------+
SELECT `id`,GROUP_CONCAT(`name` ORDER BY `name` DESC SEPARATOR ',') AS name FROM `ta` GROUP BY `id`;  

示例4

上一篇下一篇

猜你喜欢

热点阅读