常用的sql语句记录

1 查找某个字段是否唯一

1
select count(*) from table group by demo having count(*) > 1

2 交换两列的值

1
2
3
4
-- mysql
update table1 a, table1 b
set a.field_1 = b.field_2, a.field_2 = b.field_1
where a.id = b.id // 这两个字段必须一致,否则就会串行

3 json使用

1
2
3
4
5
6
7
8
9
-- json数组去重,使用json_table
select first_module, second_module, group_concat(distinct third)
from (select first_module, second_module, json_arrayagg(third_module) third_list
from cr_system_function
group by first_module, second_module) a,
json_table(third_list, '$[*]' COLUMNS (
third varchar(255) path '$'
)) b
group by first_module, second_module;
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2020-2024 zhanglonglong
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信