concat和group_concat --Mysql

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 省份;

concatgroup_concat都是在SQL语句中用于拼接字符串的函数。它们的使用方式有所不同。concat函数用于将多个字符串连接在一起,可以在行数据之间拼接。而group_concat函数则用于将列数据进行拼接,并自动生成逗号作为分隔符。 功能上,concatgroup_concat实现的效果是相似的,都是将多个字符串连接成一个字符串。但是group_concat函数可以一次性指定分隔符,而concat函数默认不会添加分隔符。如果需要在concat函数中添加分隔符,可以手动使用字符串函数例如concat_ws来实现。 在语法上,concat函数的用法是concat(字符串1, 字符串2, ...),而group_concat函数的用法是group_concat([distinct] 要连接的字段 [order by 排序字段 asc/desc] [separator '分隔符'])。group_concat函数中可以选择使用distinct关键字去除重复的值,并且可以指定排序字段和升降序排列。同时,也可以在group_concat函数中使用separator参数来自定义拼接的分隔符。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [concatgroup_concat --Mysql](https://blog.csdn.net/Theo9/article/details/102677747)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [MySQL教程之concat以及group_concat的用法](https://blog.csdn.net/sfq_bluesky/article/details/122867286)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值