***常见复杂SQL语句(含统计类SQL)

1、SQL统计某字段的出现次数

比如统计某个表中,姓名出现的次数:
select name,count(*) from biao group by name having count(*) > 2

关键是用分组:group by,且经常和聚合函数一起使用

比如:统计用户表中的匿名字段的出现次数

SELECT  a.user_anon_name as anon_name  , COUNT(a.user_anon_name) as num FROM xm_user a GROUP BY a.user_anon_name

标准写法:

select 类别, sum(数量) as 数量之和 from A
group by 类别
having sum(数量) > 18


 2、SQL中如何用一个表的列更新另一个表的列

update T2 
set 要更新的字段 = T1.对应的字段 --可以输入多个用逗号分开
from T1
where T2.ID = T1.ID


3、SQL 多字段排序
select * from (select * from tablename order by last_time desc) as t order by t.id desc




你可能感兴趣的:(***常见复杂SQL语句(含统计类SQL))