ä¸ãmysqlæ¥è¯¢çäºç§åå¥
where(æ¡ä»¶æ¥è¯¢)ãhavingï¼çéï¼ãgroup byï¼åç»ï¼ãorder byï¼æåºï¼ãlimitï¼éå¶ç»ææ°ï¼
1ãwhere常ç¨è¿ç®ç¬¦ï¼
æ¯è¾è¿ç®ç¬¦
> , < ,= , != ï¼< >ï¼,>= , <=
in(v1,v2..vn)
between v1 and v2 å¨v1è³v2ä¹é´ï¼å
å«v1,v2ï¼
é»è¾è¿ç®ç¬¦
not ( ! ) é»è¾é
or ( || ) é»è¾æ
and ( && ) é»è¾ä¸
where price>=3000 and price <= 5000 or price >=500 and price <=1000
å500-1000æè
3000-5000çå¼
where price not between 3000 and 5000
ä¸å¨3000ä¸5000ä¹é´çå¼
模ç³æ¥è¯¢
like å
éé
符:
% ä»»æå符
_ å个å符
where goods_name like '诺åºäº%'
where goods_name like '诺åºäºN__'
2ãgroup by åç»
ä¸è¬æ
åµä¸groupéä¸ç»è®¡å½æ°ï¼èåå½æ°ï¼ä¸èµ·ä½¿ç¨æææä¹
å¦ï¼select goods_id,goods_name,cat_id,max(shop_price) from goods group by cat_id;
è¿éååºæ¥çç»æä¸çgood_nameæ¯é误çï¼å 为shop_price使ç¨äºmaxå½æ°ï¼é£ä¹å®æ¯åæ大çï¼èè¯å¥ä¸ä½¿ç¨äºgroup by åç»ï¼é£ä¹goods_name并没æ使ç¨èåå½æ°ï¼å®åªæ¯cat_idä¸ç第ä¸ä¸ªååï¼å¹¶ä¸ä¼å 为shop_priceæ¹åèæ¹å
mysqlä¸çäºç§ç»è®¡å½æ°ï¼
ï¼1ï¼maxï¼æ±æ大å¼
select max(goods_price) from goods
è¿éä¼ååºæ大çä»·æ ¼çå¼ï¼åªæå¼
#æ¥è¯¢æ¯ä¸ªæ ç®ä¸ä»·æ ¼æé«ç
select cat_id,max(goods_price) from goos group by cat_id;
#æ¥åºä»·æ ¼æé«çååç¼å·
select goods_id,max(goods_price) from goods group by goods_id;
ï¼2ï¼minï¼æ±æå°å¼
ï¼3ï¼sumï¼æ±æ»æ°å
#æ±åååºåæ»å
select sum(goods_number) from goods;
ï¼4ï¼avgï¼æ±å¹³åå¼
#æ±æ¯ä¸ªæ ç®çååå¹³åä»·æ ¼
select cat_id,avg(goods_price) from goods group by cat_id;
ï¼5ï¼countï¼æ±æ»è¡æ°
#æ±æ¯ä¸ªæ ç®ä¸ååç§ç±»
select cat_id,count(*) from goods group by cat_id;
###è¦ææ¯ä¸ªå段åå½æåéæ¥ç解ï¼å®å¯ä»¥è¿è¡è¿ç®###
ä¾ï¼æ¥è¯¢æ¬åºæ¯ä¸ªååä»·æ ¼æ¯å¸åºä»·ä½å¤å°ï¼
select goods_id,goods_name,goods_price-market_price from goods;
æ¥è¯¢æ¯ä¸ªæ ç®ä¸é¢ç§¯åç货款
select cat_id,sum(goods_price*goods_number) from goods group by cat_id;
###å¯ä»¥ç¨asæ¥ç»è®¡ç®ç»æå个å«å###
select cat_id,sum(goods_price * goods_number) as hk from goods group by cat_id
ä¸ä»
ååå¯ä»¥åå«åï¼è¡¨åä¹å¯ä»¥åå«å
3ãhaving ä¸where çå¼åç¹
havingä¸where类似ï¼å¯ä»¥çéæ°æ®ï¼whereåç表达å¼æä¹åï¼havingåå°±æä¹å
whereé对表ä¸çååæ¥ä½ç¨ï¼æ¥è¯¢æ°æ®
having对æ¥è¯¢ç»æä¸çååæ¥ä½ç¨ï¼çéæ°æ®
#æ¥è¯¢æ¬åºååä»·æ ¼æ¯å¸åºä»·ä½å¤å°é±ï¼è¾åºä½200å
以ä¸çåå
select goods_id,good_name,market_price - shop_price as s from goods having s>200 ;
//è¿éä¸è½ç¨whereå 为sæ¯æ¥è¯¢ç»æï¼èwhereåªè½å¯¹è¡¨ä¸çå段åçé
å¦æç¨whereçè¯åæ¯ï¼
select goods_id,goods_name from goods where market_price - shop_price > 200;
#åæ¶ä½¿ç¨whereä¸having
select cat_id,goods_name,market_price - shop_price as s from goods where cat_id = 3 having s > 200;
#æ¥è¯¢ç§¯å货款è¶
è¿2ä¸å
çæ ç®ï¼ä»¥å该æ ç®ç§¯åç货款
select cat_id,sum(shop_price * goods_number) as t from goods group by cat_id having s > 20000
#æ¥è¯¢ä¸¤é¨å两é¨ä»¥ä¸ç§ç®ä¸åæ ¼çå¦ççå¹³åå
æè·¯ï¼
#å
计ç®ææå¦ççå¹³åå
select name,avg(score) as pj from stu group by name;
#æ¥åºææå¦ççæç§æ
åµ
select name,score<60 from stu;
#è¿éscore<60æ¯å¤æè¯å¥ï¼æ以ç»æ为çæåï¼mysqlä¸ç为1å为0
#æ¥åºä¸¤é¨å两é¨ä»¥ä¸ä¸åæ ¼çå¦ç
select name,sum(score<60) as gk from stu group by name having gk > 1;
#综åç»æ
select name,sum(score<60) as gk,avg(score) as pj from stu group by name having gk >1;
4ãorder by
ï¼1ï¼ order by price //é»è®¤ååºæå
ï¼2ï¼order by price desc //éåºæå
ï¼3ï¼order by price asc //ååºæåï¼ä¸é»è®¤ä¸æ ·
ï¼4ï¼order by rand() //éæºæåï¼æçä¸é«
#ææ ç®å·ååºæåï¼æ¯ä¸ªæ ç®ä¸çååä»·æ ¼éåºæå
select * from goods where cat_id !=2 order by cat_id,price desc;
5ãlimit
limit [offset,] N
offset å移éï¼å¯éï¼ä¸ååç¸å½äºlimit 0,N
N ååºæ¡ç®
#åä»·æ ¼ç¬¬4-6é«çåå
select good_id,goods_name,goods_price from goods order by good_price desc limit 3,3;
###æ¥è¯¢æ¯ä¸ªæ ç®ä¸æè´µçåå
æè·¯ï¼
#å
对æ¯ä¸ªæ ç®ä¸çååä»·æ ¼æåº
select cat_id,goods_id,goods_name,shop_price from goods order by cat_id,shop_price desc;
#ä¸é¢çæ¥è¯¢ç»æä¸æ¯ä¸ªæ ç®ç第ä¸è¡çååå°±æ¯æè´µçåå
#æä¸é¢çæ¥è¯¢ç»æç解为ä¸ä¸ªä¸´æ¶è¡¨[åå¨äºå
åä¸]ãåæ¥è¯¢ã
#åä»ä¸´æ¶è¡¨ä¸éåºæ¯ä¸ªæ ç®æè´µçåå
select * from (select goods_id,goods_name,cat_id,shop_price from goods order by cat_id,shop_price desc) as t group by cat_id;
#è¿é使ç¨group by cat_idæ¯å 为临æ¶è¡¨ä¸æ¯ä¸ªæ ç®ç第ä¸ä¸ªååå°±æ¯æè´µçååï¼ègroup byåé¢æ²¡æ使ç¨èåå½æ°ï¼æ以é»è®¤å°±åæ¯ä¸ªåç»ç第ä¸è¡æ°æ®ï¼è¿é以cat_idåç»
è¯å¥½çç解模åï¼
1ãwhereåé¢ç表达å¼ï¼æ表达å¼æ¾å¨æ¯ä¸è¡ä¸ï¼çæ¯å¦æç«
2ãå段(å)ï¼ç解为åéï¼å¯ä»¥è¿è¡è¿ç®ï¼ç®æ¯è¿ç®åé»è¾è¿ç®ï¼
3ã ååºç»æå¯ä»¥ç解æä¸å¼ 临æ¶è¡¨
äºãmysqlåæ¥è¯¢
1ãwhereååæ¥è¯¢
ï¼æå
å±æ¥è¯¢ç»æå½ä½å¤å±æ¥è¯¢çæ¯è¾æ¡ä»¶ï¼
#ä¸ç¨order by æ¥æ¥è¯¢ææ°çåå
select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods);
#ååºæ¯ä¸ªæ ç®ä¸ææ°ç产å(goods_idå¯ä¸)
select cat_id,goods_id,goods_name from goods where goods_id in(select max(goods_id) from goods group by cat_id);
2ãfromååæ¥è¯¢
(æå
å±çæ¥è¯¢ç»æä¾å¤å±å次æ¥è¯¢)
#ç¨åæ¥è¯¢æ¥åºæç§ä¸¤é¨å以ä¸çåå¦çå¹³åæ绩
æè·¯ï¼
#å
æ¥åºåªäºåå¦æç§ä¸¤é¨ä»¥ä¸
select name,count(*) as gk from stu where score < 60 having gk >=2;
#以ä¸æ¥è¯¢ç»æï¼æ们åªè¦ååå°±å¯ä»¥äºï¼æ以ååä¸æ¬¡åå
select name from (select name,count(*) as gk from stu having gk >=2) as t;
#æ¾åºè¿äºåå¦äºï¼é£ä¹å计ç®ä»ä»¬çå¹³åå
select name,avg(score) from stu where name in (select name from (select name,count(*) as gk from stu having gk >=2) as t) group by name;
3ãexistsååæ¥è¯¢
ï¼æå¤å±æ¥è¯¢ç»ææ¿å°å
å±ï¼çå
å±çæ¥è¯¢æ¯å¦æç«ï¼
#æ¥è¯¢åªäºæ ç®ä¸æååï¼æ ç®è¡¨category,åå表goods
select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id);
ä¸ãunionçç¨æ³
ï¼æ两次æå¤æ¬¡çæ¥è¯¢ç»æå并起æ¥ï¼è¦æ±æ¥è¯¢çåæ°ä¸è´ï¼æ¨èæ¥è¯¢ç对åºçåç±»åä¸è´ï¼å¯ä»¥æ¥è¯¢å¤å¼ 表ï¼å¤æ¬¡æ¥è¯¢è¯å¥æ¶å¦æååä¸ä¸æ ·ï¼åå第ä¸æ¬¡çååï¼å¦æä¸åçè¯å¥ä¸ååºçè¡çæ¯ä¸ªåçå¼é½ä¸æ ·ï¼é£ä¹ç»æå°èªå¨ä¼å»éå¤ï¼å¦æä¸æ³å»éå¤åè¦å allæ¥å£°æï¼å³union allï¼
## ç°æ表aå¦ä¸
id num
a 5
b 10
c 15
d 10
表bå¦ä¸
id num
b 5
c 10
d 20
e 99
æ±ä¸¤ä¸ªè¡¨ä¸idç¸åçå
select id,sum(num) from (select * from ta union select * from tb) as tmp group by id;
//以ä¸æ¥è¯¢ç»æå¨æ¬ä¾ä¸çç¡®è½æ£ç¡®è¾åºç»æï¼ä½æ¯ï¼å¦æætbä¸çbçå¼æ¹ä¸º10以æ¥è¯¢ç»æçbçå¼å°±æ¯10äºï¼å 为taä¸çbä¹æ¯10ï¼æ以unionåä¼è¢«è¿æ»¤æä¸ä¸ªéå¤çç»æï¼è¿æ¶å°±è¦ç¨union all
select id,sum(num) from (select * from ta union all select * from tb) as tmp group by id;
#å第4ã5æ ç®çååï¼ææ ç®ååºæåï¼æ¯ä¸ªæ ç®çååä»·æ ¼éåºæåï¼ç¨unionå®æ
select goods_id,goods_name,cat_id,shop_price from goods where cat_id=4 union select goods_id,goods_name,cat_id,shop_price from goods where cat_id=5 order by cat_id,shop_price desc;
ãå¦æåå¥ä¸æorder by éè¦ç¨( ) å
èµ·æ¥ï¼ä½æ¯æ¨èå¨æå使ç¨order byï¼å³å¯¹æç»å并åçç»ææ¥æåºã
#å第3ã4个æ ç®ï¼æ¯ä¸ªæ ç®ä»·æ ¼æé«çå3个ååï¼ç»ææä»·æ ¼éåºæå
(select goods_id,goods_name,cat_id,shop_price from goods where cat_id=3 order by shop_price desc limit 3) union (select goods_id,goods_name,cat_id,shop_price from goods where cat_id=4 order by shop_price desc limit 3) order by shop_price desc;
åãå·¦è¿æ¥ï¼å³è¿æ¥ï¼å
è¿æ¥
ç°æ表aæ10æ¡æ°æ®ï¼è¡¨bæ8æ¡æ°æ®ï¼é£ä¹è¡¨aä¸è¡¨bçç¬å°å¡ç§¯æ¯å¤å°ï¼
select * from ta,tb //è¾åºç»æ为8*10=80æ¡
1ãå·¦è¿æ¥
以左表为åï¼å»å³è¡¨æ¾æ°æ®ï¼å¦æ没æå¹é
çæ°æ®ï¼å以null补空ä½ï¼æ以è¾åºç»ææ°>=左表åæ°æ®æ°
è¯æ³ï¼select n1,n2,n3 from ta left join tb on ta.n1= ta.n2 [è¿éonåé¢ç表达å¼ï¼ä¸ä¸å®ä¸º=ï¼ä¹å¯ä»¥>ï¼<çç®æ¯ãé»è¾è¿ç®ç¬¦]ãè¿æ¥å®æåï¼å¯ä»¥å½æä¸å¼ æ°è¡¨æ¥çå¾
ï¼è¿ç¨whereçæ¥è¯¢ã
#ååºä»·æ ¼æé«çäºä¸ªååï¼å¹¶æ¾ç¤ºååçåç±»å称
select goods_id,goods_name,goods.cat_id,cat_name,shop_price from goods left join category on goods.cat_id = category.cat_id order by shop_price desc limit 5;
2ãå³è¿æ¥
a left join b çä»·äº b right join a
æ¨è使ç¨å·¦è¿æ¥ä»£æ¿å³è¿æ¥
è¯æ³ï¼select n1,n2,n3 from ta right join tb on ta.n1= ta.n2
3ãå
è¿æ¥
æ¥è¯¢ç»ææ¯å·¦å³è¿æ¥ç交éï¼ãå³å·¦å³è¿æ¥çç»æå»é¤null项åç并éï¼å»é¤äºéå¤é¡¹ï¼ã
mysqlç®åè¿ä¸æ¯æ å¤è¿æ¥ï¼å³å·¦å³è¿æ¥ç»æç并é,ä¸å»é¤null项ï¼
è¯æ³ï¼select n1,n2,n3 from ta inner join tb on ta.n1= ta.n2
æ»ç»ï¼å¯ä»¥å¯¹åä¸å¼ 表è¿æ¥å¤æ¬¡ï¼ä»¥åå«åå¤æ¬¡æ°æ®
追é®æéè¦çæ¯ç¨mysqlè¯å¥æ¥è¯¢åºååæ¯å¦æ¯å½å£ååºï¼ï¼ï¼ä¸¾ä¸ªä¾åï¼ï¼æ件货åæ¯å¨2015-12-1å·ä¸å¸ï¼ï¼å¦æååå¨2015-12-1å°2016-2-29å·ä¹é´ååºå°±æ¯å±äºæ°å£éå®é¢ï¼å¦ä½éè¿mysqlæ¥æ¾åºæ以çæ°è´§éå®é¢ï¼