常用sql

1、去重

SELECT count(DISTINCT sourcip) FROM webids_alarm;   # 统计不同的源IP个数

select count(distinct concat(LastName,FirstName)) from  Person; # 多列合并,然后去重,最后计数;

SELECT sourcip FROM ips_alarm union SELECT dst_ip FROM ips_alarm;   # 选取不同的源IP和目的IP,注:uinon可以去重,union all不去重

2、计数
SELECT count(1) as total,      # 全部计数
count(case when table='webids_alarm' then '1' end) as webids_alarm,    # table为webids_alarm的条目计数
count(case when table='ips_alarm' then '1' end) as ips_alarm,   # table为webids_alarm的条目计数
FROM alarm_all_alarm;

3、分组+计数

SELECT table,ruleid,count(times)
FROM alarm_all_alarm
GROUP BY table,ruleid
HAVING count(times)>1;     # 选取次数大于1的id  注:having用于分组后再过滤

4、分组+排序

SELECT table,threat_type,count(1)

FROM alarm_all_alarm 

GROUP BY table,threat_type

ORDER BY table,threat_type;  #在分组后,搭配排序,这样展示数据时,先按照第一列排序,然后按照第二列排序

 5、分组+取单一值

数据表有AB两列。A列分组,每组只取一个,同时要取出A对应的B的值。

select A,min(B) from my_table  GROUP BY A;

6、选取并修改数据

select concat(employee,title) from jobs;  连接两个字符串

select concat('0x',to_hex(num)) from alarm_table; 10进制转为16进制

 

posted @ 2022-11-16 17:43  超级宝宝11  阅读(35)  评论(0编辑  收藏  举报