mysql的distinct,count,in 和 offset 使用

1. distinct:去重

# distinct
select distinct name_adress from my_test_copy;   # 得到去重字段
select count(distinct name_adress) as distinct_rows from my_test_copy; #对某一列去重后统计
select distinct id, name_adress from my_test_copy;   # 得到去重字段,但是此时同时作用于两个字段,也就是只要有一个不一样就行,同时一样的才不会取出来

# select id, distinct name_adress from my_test_copy; # 不能这样写,因为distinct要放在开头

2 count:统计

# count
select count(*) as num_rows from my_test_copy;   # as:重命名
select count(1) from my_test_copy;               # 统计记录数,包括NULL值记录

3. in:判断属于特性

# in 和 not in
select * from my_test_copy where name_adress in ('北京','南京');  #枚举需要的列
select * from my_test_copy where name_adress not in ('北京','南京');  #枚举不需要的列

4. offset:偏置量,表示跳过多少行

select * from my_test_copy limit 2 offset 2; #跳过前两行

# 欢迎交流

猜你喜欢

转载自www.cnblogs.com/qi-yuan-008/p/12198839.html