数据库
首页 > 数据库> > sql总结

sql总结

作者:互联网

1.列转行

  表名:test

  id,name,gender

  1,老王,男

  2,老李,男

  3,小张,女

  select gender,concat_ws(collect_set(name),'-') FROM test group by gender;

  结果:

  男,老王-老李

  女,小张

  collect_set会去重,如果不想去重可以用collect_list

2.正则

  select 'test' rlike '\w+';

  查看表名时过滤后like不是用正则也不是用%,而是*

3.两个表差异比较

  A表

  id

  1

  2

 

  B表

  id

  2

  3

 

  select count(1),count(a.id),count(b.id),count(IF(a.id is not null and b.id is not null,1,null)) as ab,count(IF(a.id is null and b.id is not null,1,null)) as only_b,count(IF(a.id is not null and b.id is null,1,null)) as only_a from a full outer join b on a.id=b.id;

 

  其中ab 是两个表都有的,

  only_b 是只有b中有

  only_a 是只有a中有

标签:总结,count,only,sql,test,null,id,select
来源: https://www.cnblogs.com/yangyang12138/p/16695183.html