版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hzhj2007/article/details/87296612
主要针对查询功能进行说明
- 去重
select picfilepath, specid, collect_set(seriesid) from $TABLENAME where picclass=1 group by picfilepath, specid ; #去重后输出多字段
- 升序降序
select `picfilepath`, count(*) as num from $TABLENAME where picclass=1 group by picfilepath order by num; #默认去重并按照键值数量num升序
select `picclass`, count(*) as num from $TABLENAME group by picclass order by picclass; #去重并按照字段排序
select `picfilepath`, count(*) as num from $TABLENAME where picclass=1 group by picfilepath order by num asc; #使用asc升序关键字
select `picfilepath`, count(*) as num from $TABLENAME where picclass=1 group by picfilepath order by num desc; #使用desc降序关键字
- 数量查找
select `picfilepath`, count(*) as num from $TABLENAME where picclass=1 group by picfilepath ; #重复字段的数量
select seriesid,count(picid) as num from $TABLENAME group by seriesid having num <10000 order by num ; #数量阈值:picid超过10000的值
- 限制数量的查找
select picclass, count(picclass) as num from $TABLENAME group by picclass order by num limit 0, 1; #最少数量的
select picclass, count(picclass) as num from $TABLENAME group by picclass order by num desc limit 0, 1; #最多数量的
select picclass, count(picclass) as num from $TABLENAME group by picclass order by num limit 0, 4; #最少数量的前4个
- 区间查找
select seriesid,count(picid) as num from $TABLENAME where picclass=1 group by seriesid having num between 10000 and 30000 order by num ; #num位于[1w, 3w]的值
select seriesid,count(picid) as num from $TABLENAME where picclass between 1 and 3 group by seriesid order by num ; #字段picclass位于[1, 3]区间的值
- 联表查询
SELECT p.picfilepath ,p.seriesid, q.series_name,p.specid
FROM $TABLENAMEA as p left join $TABLENAMEB as q
on p.seriesid = q.series_id
where p.picclass=1
order by p.seriesid, p.specid, q.series_name; #联表查询seriesid和series_id字段相等的值
select series_id,count(picid) as num
from(select b.series_id as series_id,a.picid as picid
from (select picid,seriesid from $TABLENAMEA where picclass=1) a join
(select * from $TABLENAMEB where series_is_public=1) b on a.seriesid=b.series_id) c
group by series_id
order by num;
参考文献: