1、concat和group_concat都是用在sql语句中做拼接使用的,但是两者使用的方式不尽相同,concat是针对以行数据做的拼接,而group_concat是针对列做的数据拼接,且group_concat自动生成逗号。
2、concat的使用
结果
3、group_concat的使用:
结果:
而且group_concat一般和group by 结合使用比较多
彩蛋:根据身份证号查询出每个省有多少人
select case left(up.identity,2)
when '11' then '北京市'
when '12' then '天津市'
when '13' then '河北省'
when '14' then '山西省'
when '15' then '内蒙古自治区'
when '21' then '辽宁省'
when '22' then '吉林省'
when '23' then '黑龙江省'
when '31' then '上海市'
when '32' then '江苏省'
when '33' then '浙江省'
when '34' then '安徽省'
when '35' then '福建省'
when '36' then '江西省'
when '37' then '山东省'
when '41' then '河南省'
when '42' then '湖北省'
when '43' then '湖南省'
when '44' then '广东省'
when '45' then '广西壮族自治区'
when '46' then '海南省'
when '50' then '重庆市'
when '51' then '四川省'
when '52' then '贵州省'
when '53' then '云南省'
when '54' then '西藏自治区'
when '61' then '陕西省'
when '62' then '甘肃省'
when '63' then '青海省'
when '64' then '宁夏回族自治区'
when '65' then '新疆维吾尔自治区'
when '71' then '台湾省'
when '81' then '香港特别行政区'
when '82' then '澳门特别行政区'
else '未知'
end as 省份,
count(acr.id) as 人数
FROM
article_call_record as acr
LEFT JOIN qydproduction.user_profile AS up ON acr.user_id = up.id
where acr.create_time>='2019-10-17 12:00:00' and acr.create_time<'2019-10-18 12:00:00'
GROUP BY 省份;