mysql子句查询
mysql的select查询一共有5种子句查询,包括where,group by,having,order by,limit,在select语句中可以使用一种或多种子句查询,但如果要使用多种,必须要按where,group by,having,order by,limit的顺序来书写。
where子句查询
where后面跟的是条件,比如,select goods_id,goods_name from goods where goods_id>5 and goods_id<10;
group by子句查询
group by是分组查询,一般是和max(),min(),sum(),count(),avg(),比如,select avg(shop_price),cat_id from goods group by cat_id;
having子句查询
having是筛选查询,比如,select goods_id,cat_id,goods_name,(mark_price-shop_price) as sheng from goods having sheng >100; 注意这里不能用where sheng,因为where是针对磁盘上的数据文件,select语句选出来的是结果集,储存在内存上,此时的sheng列是存在结果集里的,不是磁盘上的,所以where是找不到sheng列的,having是针对结果集的。
order by子句查询
order by是排序查询,默认是升序asc,降序是desc,可以是多列排序,比如,select goods_id,goods_name,cat_id,shop_price from goods where cat_id<=3 order by cat_id asc,shop_price desc;
limit子句查询
limit是限制取出条目的,一般是配合order使用,通常用在分页上,比如,select goods_id,cat_id,goods_name,shop_price from goods order by shop_price desc limit 2,5;