1、group_concat函数:用于将多个字符串连接成一个字符串
用法规则:
SELECT GROUP_CONCAT(拼接的字段) from tableName;
使用:查询basic_project_vendor表的vendor_name字段,拼接成字符串返回
SELECT GROUP_CONCAT(vendor_name) from basic_project_vendor
使用:查询basic_project_vendor表且主键等于id1,id2的vendor_name字段,拼接成字符串返回
SELECT GROUP_CONCAT(vendor_name) as name from basic_project_vendor where pk_project_vendor IN ('id1','id2')
2、substring_index函数:截取字符串
用法规则:
substring_index(“待截取字符串”,“截取依据的字符”,截取依据字符的位置N)
具体例子:
假设待截取字符串为:"12,13,14,15,16,17"
截取依据的字符:","
具体要截取第N个逗号的字符串;
意思就是:在字符串中以逗号为索引,获取不同索引位的字符;
结果如下:
2.1)取第1个逗号前的字符:
SELECT SUBSTRING_INDEX("12,13,14,15,16,17",",",1);
执行结果为:12
2.2)N可以为负数,表示倒数第N个索引字符后面的字符串(有负号的时候,可以将整个字符倒过来看,依旧是第N个字符前面的部分)
SELECT SUBSTRING_INDEX("12,13,14,15,16,17",",",-1);
执行结果:17
2.3)可以将截取的结果再当成字符串,比如:取第2个逗号前那部分字符串里的最后逗号后面的那部分
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX("12,13,14,15,16,17",",",2),",",-1);
执行结果:13
3、order by :后面跟多个字段英文逗号隔开,默认是asc升序(desc是 降序)
a 字段用升序,b 字段用降序,sql应这样写:
ORDER BY a,b desc 或者 ORDER BY a asc,b desc
若两个都是降序,sql应这样写:
ORDER BY a desc,b desc;(两个都要desc)
如下:查询basic_project_vendor表,并依次按cyear,batch,ts字段降序排列
SELECT * from basic_project_vendor where IFNULL(jl_is_del,0)=0 and isremove='Y' and ( vendor_name like '%明%' ) ORDER BY cyear desc,batch desc,ts desc