查找SELECT函數
group分組與統計函數spa
max(), min(),count(),avg(),sum()排序
例子:SELECT avg(`shop_price`)FROM `goods`
SELECT avg(shop_price) FROM goods group by cat_id;
數學
模糊查找it
SELECT * FROM goods WHERE name like 「諾基亞%」select
數學計算 floor(),ceil(),等。im
篩選having統計
select goods_name (market_price-shop_price) as sheng where having sheng>200鏈表
order by 排序項目
select goods_name ,shop_price FROM goods order by shop_price;(升序)
select goods_name ,shop_price FROM goods order by shop_price desc;(降序)
選出價格最高的三個商品
select goods_name ,shop_price FROM goods order by shop_price desc limit 3;
select goods_name ,shop_price FROM goods order by shop_price desc limit 3,2;(跳過3行,取2行)。
where子查詢
SELECT goods_id,goods_name FROM goods WHERE goods_id in (SELECT max(goods_id) FROM goods group by cat_id);便可選出每個項目的最大值。
7.from子查詢,把查詢到的結果做爲新的表進行查詢。
SELECT * FROM (SELECT goods_id ,goods_name,cat_id FROM goods group by cat_id) as tmp
8.exists子查詢,在不一樣表之間查詢
SELECT * FROM categroy WHERE exists(SELECT * FROM goods WHERE goods.cat_id=categroy.cat_id );
9.內鏈表 inner join 鏈接兩張表
SELECT boy.hid,boy.bname,girl.hid,girl.gname from boy inner join girl on boy.hid=girl.hid
10.左右內鏈
SELECT boy.hid,boy.bname,girl.hid,girl.gname from boy left join girl on boy.hid=girl.hid
SELECT boy.hid,boy.bname,girl.hid,girl.gname from boy rightjoin girl on boy.hid=girl.hid