select * from user; awk 1 user; select * from consumer where cost > 100; awk '$2>100' consumer
去重 distinct
1 2 3 4
select distinct(date) from consumer; awk '!a[$3]++ {print $3}' consumer select distinct(*) from consumer; awk '!a[$0]++' consumer
这里新建了一个变量数组 a[],当 a 里没有 key 的时候打印,即第一次打印,余下都跳过
排序 order by
1 2
select id from user order by id; awk '{a[$1]}END{asorti(a);for(i=1;i<=length(a);i++) {print a[i]}}' user
limit
1 2 3
select * from consumer limit 2; awk 'NR<=2' consumer awk 'NR>2{exit}1' consumer # performance is better
分组求和统计,关键词:group by、having、sum、count
1 2
select id, count(1), sum(cost) from consumer group by id having count(1) > 2; awk '{a[$1]=a[$1]==""?$2:a[$1]","$2}END{for(i in a){c=split(a[i],b,",");if(c>2){sum=0;for(j in b){sum+=b[j]};print i"\t"c"\t"sum}}}' consumer
模糊查询,关键词:like(like属于通配,也可正则 REGEXP)
1 2 3 4 5 6
select name from user where name like 'wang%'; awk '$2 ~/^wang/{print $2}' user select addr from user where addr like '%bei'; awk '/.*bei$/{print $3}' user select addr from user where addr like '%bei%'; awk '$3 ~/bei/{print $3}' user
多表 join 关联查询,关键词:join
1 2
select a.* , b.* from user a inner join consumer b on a.id = b.id and b.id = 2; awk 'ARGIND==1{a[$1]=$0;next}{if(($1 in a)&&$1==2){print a[$1]"\t"$2"\t"$3}}' user consumer
多表水平联接,关键词:union all
1 2 3 4
select a.* from user a union all select b.* from user b; awk 1 user user select a.* from user a union select b.* from user b; awk '!a[$0]++' user user
随机抽样统计,关键词:order by rand()
1 2
SELECT * FROM consumer ORDER BY RAND() LIMIT 2; awk 'BEGIN{srand();while(i<2){k=int(rand()*10)+1;if(!(k in a)){a[k];i++}}}(NR in a)' consumer