单表查询总结

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

  

posted @ 2020-06-10 17:01  木子李和三点水  阅读(279)  评论(0编辑  收藏  举报