单表查询总结

Posted liyanglin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单表查询总结相关的知识,希望对你有一定的参考价值。

/*查询一张表所有的数据*/
select * from mall_goods;
/*查询某些列*/
select id,name,goodsno from mall_goods;
/*为列取别名,为了能与JavaBean中的属性对应,区分多张表中同名的列*/
select id as goodsId,name goodsname,goodsno from mall_goods;

/*查询条件*/
select * from mall_goods where id=518;
select * from mall_goods where newest=‘是‘;
select * from mall_goods where hot=‘是‘;
select * from mall_goods where categoryid=1 and newest=‘是‘;
select * from mall_goods where stock>100 and stock<200;
select * from mall_goods where stock between 100 and 200;  /*是否包含100,200 */


/*排序,默认是升序,可以使用desc和asc明确是降序和升序*/
select * from mall_goods order by stock desc,goodsno desc;  

/*模糊查询*/
select * from mall_goods where name like ‘%IT%‘;
select * from mall_goods where name like ‘IT%‘;
select * from mall_goods where name like ‘IT_‘;

/*聚合查询*/
select count(*) from mall_goods;
select sum(stock) from mall_goods;
select max(stock) from mall_goods;
select min(stock) from mall_goods;
select avg(salesprice) from mall_goods;

/*分组:查询每一种类别图书的总数*/
select categoryid,count(1) from mall_goods group by categoryid;

/*分组:查询图书数在150以上的*/
select categoryid,count(1) from mall_goods group by categoryid having count(1)>150;

/*分组:查询所有最新商品,图书总数在15之上的分组统计*/
select categoryid,count(1) from mall_goods where newest=‘是‘ 
group by categoryid having count(1)>15 order by count(1) desc;

/*分页查询*/
SELECT * FROM mall_goods LIMIT 10;//查询前十条数据
SELECT * FROM mall_goods LIMIT 0,10;//(开始标记,每页条数)第一页<1开始><10条数据>
SELECT * FROM mall_goods LIMIT 10,10;//第二页<11开始><10条数据>

  

以上是关于单表查询总结的主要内容,如果未能解决你的问题,请参考以下文章

单表查询总结

3.MySQL优化---单表查询优化的一些小总结(非索引设计)

SQL单表查询练习部分总结

「mysql优化专题」单表查询优化的一些小总结,非索引设计

Django框架05 /orm单表操作

MySQL基础语法之单表查询