一、select查询
where?表達(dá)式
表達(dá)式在哪一行成立,哪一行就被取出來
=,!=,<>,>,<,>=,<=,in,between?and?,or?,not
group?by
分組,一般和統(tǒng)計函數(shù)配合使用
max,min,avg,sum,count
查出價格最高的商品價格:select?cat_id,max(shop_price)?from?ecs_goods;
查出價格最便宜的商品價格:select?cat_id,min(shop_price)?from?ecs_goods;
查詢該店一共有多少商品:select?count(*)?from?ecs_goods;
查詢該店庫存總量:select?sum(goods_number)?from?ecs_goods;
查出每個欄目價格最高的產(chǎn)品價格:select?cat_id,max(shop_price)?from?ecs_goods?group?by?cat_id;
查出每個欄目下積壓的貨款:
having表達(dá)式
數(shù)據(jù)在表中存儲,表存在硬盤或內(nèi)存中以文件形式存在
where就是針對表文件發(fā)揮作用,
where查詢出來的結(jié)果可以看成是一張表,其文件一般臨時存在緩沖區(qū)
having是針對查詢的結(jié)果發(fā)揮的作用
作用:排序
可以針對字段,升序降序排列,
有可能一個字段查不出來結(jié)果,可以選擇其他字段繼續(xù)排序
查詢欄目為3的賣價比市場價便宜200元的產(chǎn)品
>?select?goods_id,cat_id,goods_name,market_price?-?shop_price?as?nowPrice?from??ecs_goods??where?cat_id?=?3?having?nowPrice?>?200?;
order?by
???查詢某個欄目下積壓貨款大于2w的欄目以及該欄目下積壓的貨款總和
???>?select?cat_id,sum(shop_price*goods_number)?as?k?from?ecs_goods?group?by?cat_id?having?k?>?20000;
???查詢出每人掛科兩門及兩門以上和不及格的平均分
???>select?name,sum(chengji?<?60?)?as?gk,avg(chengji)?as?pj?from?fenshu?group?by?name?having?gk>=2
???
limit?偏移量,取出的條目
可以把列名當(dāng)做變量看待,可以使用數(shù)學(xué)公式進(jìn)行計算
????select?cat_id,sum(shop_price*goods_number)?from?ecs_goods?group?by?cat_id;
總結(jié):
子查詢
1、where型查詢
? 內(nèi)層的查詢結(jié)果作為外層查詢的比較條件
查詢每個欄目價格最高的產(chǎn)品:
>select?*?from?(select?goods_id,goods_name,shop_price,cat_id?from?goods?order?by?cat_id?asc,shop_price?desc)?as?tmp?group?by?cat_id
2、from型子查詢
把內(nèi)層的查詢結(jié)果供外層再次查詢
注意,內(nèi)層的查詢結(jié)果看成臨時表,加"as??臨時表"
3、exists型查詢
把外層的查詢結(jié)果帶入到內(nèi)層,看內(nèi)層是否成立
select?*?from?category?where?exists(select?*?from?goods?where?goods.cat_id?=?category.cate_id);
查詢有商品的欄目:
???>select?cat_id,cat_name?from?ecs_category?where?exists?(select?*?from?ecs_goods?where?ecs_goods.cat_id?=?ecs_category.cat_id);
總結(jié)
以上是生活随笔為你收集整理的一、select查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: header(Content-Type:
- 下一篇: union(联合)合并查询结果