码迷,mamicode.com
首页 > 其他好文 > 详细

单表查询总结

时间:2020-06-10 17:14:16      阅读:45      评论:0      收藏:0      [点我收藏+]

标签:having   class   hot   toc   where   默认   highlight   ice   mit   

/*查询一张表所有的数据*/
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条数据>

  

单表查询总结

标签:having   class   hot   toc   where   默认   highlight   ice   mit   

原文地址:https://www.cnblogs.com/liyanglin/p/13086535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!