MYSQL的select 學(xué)習(xí)筆記
更新時(shí)間:2007年02月20日 00:00:00 作者:
記錄一些select的技巧:
1、select語(yǔ)句可以用回車分隔
$sql="select * from article where id=1"
和 $sql="select * from article
where id=1",都可以得到正確的結(jié)果,但有時(shí)分開(kāi)寫(xiě)或許能更明了一點(diǎn),特別是當(dāng)sql語(yǔ)句比較長(zhǎng)時(shí)
2、批量查詢數(shù)據(jù)
可以用in來(lái)實(shí)現(xiàn)
$sql="select * from article where id in(1,3,5)"
3、使用concat連接查詢的結(jié)果
$sql="select concat(id,"-",con) as res from article where id=1"
返回"1-article content"
4、使用locate
用法:select locate("hello","hello baby");返回1
不存在返回0
5、使用group by
以前一直沒(méi)怎么搞明group by 和 order by,其實(shí)也滿簡(jiǎn)單的,group by 是把相同的結(jié)果編為一組
exam:$sql="select city ,count(*) from customer group by city";
這句話的意思就是從customer表里列出所有不重復(fù)的城市,及其數(shù)量(有點(diǎn)類似distinct)
group by 經(jīng)常與AVG(),MIN(),MAX(),SUM(),COUNT()一起使用
6、使用having
having 允許有條件地聚合數(shù)據(jù)為組
$sql="select city,count(*),min(birth_day) from customer
group by city having count(*)>10";
這句話是先按city歸組,然后找出city地?cái)?shù)量大于10的城市
btw:使用group by + having 速度有點(diǎn)慢
同時(shí)having子句包含的表達(dá)式必須在之前出現(xiàn)過(guò)
7、組合子句
where、group by、having、order by(如果這四個(gè)都要使用的話,一般按這個(gè)順序排列)
8、使用distinct
distinct是去掉重復(fù)值用的
$sql="select distinct city from customer order by id desc";
這句話的意思就是從customer表中查詢所有的不重復(fù)的city
9、使用limit
如果要顯示某條記錄之后的所有記錄
$sql="select * from article limit 100,-1";
10、多表查詢
$sql="select user_name from user u,member m
where u.id=m.id and
m.reg_date>=2006-12-28
order by u.id desc"
注意:如果user和member兩個(gè)標(biāo)同時(shí)有user_name字段,會(huì)出現(xiàn)mysql錯(cuò)誤(因?yàn)閙ysql不知道你到底要查詢哪個(gè)表里的user_name),必須指明是哪個(gè)表的;
1、select語(yǔ)句可以用回車分隔
$sql="select * from article where id=1"
和 $sql="select * from article
where id=1",都可以得到正確的結(jié)果,但有時(shí)分開(kāi)寫(xiě)或許能更明了一點(diǎn),特別是當(dāng)sql語(yǔ)句比較長(zhǎng)時(shí)
2、批量查詢數(shù)據(jù)
可以用in來(lái)實(shí)現(xiàn)
$sql="select * from article where id in(1,3,5)"
3、使用concat連接查詢的結(jié)果
$sql="select concat(id,"-",con) as res from article where id=1"
返回"1-article content"
4、使用locate
用法:select locate("hello","hello baby");返回1
不存在返回0
5、使用group by
以前一直沒(méi)怎么搞明group by 和 order by,其實(shí)也滿簡(jiǎn)單的,group by 是把相同的結(jié)果編為一組
exam:$sql="select city ,count(*) from customer group by city";
這句話的意思就是從customer表里列出所有不重復(fù)的城市,及其數(shù)量(有點(diǎn)類似distinct)
group by 經(jīng)常與AVG(),MIN(),MAX(),SUM(),COUNT()一起使用
6、使用having
having 允許有條件地聚合數(shù)據(jù)為組
$sql="select city,count(*),min(birth_day) from customer
group by city having count(*)>10";
這句話是先按city歸組,然后找出city地?cái)?shù)量大于10的城市
btw:使用group by + having 速度有點(diǎn)慢
同時(shí)having子句包含的表達(dá)式必須在之前出現(xiàn)過(guò)
7、組合子句
where、group by、having、order by(如果這四個(gè)都要使用的話,一般按這個(gè)順序排列)
8、使用distinct
distinct是去掉重復(fù)值用的
$sql="select distinct city from customer order by id desc";
這句話的意思就是從customer表中查詢所有的不重復(fù)的city
9、使用limit
如果要顯示某條記錄之后的所有記錄
$sql="select * from article limit 100,-1";
10、多表查詢
$sql="select user_name from user u,member m
where u.id=m.id and
m.reg_date>=2006-12-28
order by u.id desc"
注意:如果user和member兩個(gè)標(biāo)同時(shí)有user_name字段,會(huì)出現(xiàn)mysql錯(cuò)誤(因?yàn)閙ysql不知道你到底要查詢哪個(gè)表里的user_name),必須指明是哪個(gè)表的;
您可能感興趣的文章:
- Mysql存儲(chǔ)過(guò)程學(xué)習(xí)筆記--建立簡(jiǎn)單的存儲(chǔ)過(guò)程
- MySQL定時(shí)器EVENT學(xué)習(xí)筆記
- MySQL學(xué)習(xí)筆記5:修改表(alter table)
- MySQL學(xué)習(xí)筆記4:完整性約束限制字段
- MySQL學(xué)習(xí)筆記3:表的基本操作介紹
- MySQL學(xué)習(xí)筆記2:數(shù)據(jù)庫(kù)的基本操作(創(chuàng)建刪除查看)
- MySQL學(xué)習(xí)筆記1:安裝和登錄(多種方法)
- MySql官方手冊(cè)學(xué)習(xí)筆記2 MySql的模糊查詢和正則表達(dá)式
- MySql官方手冊(cè)學(xué)習(xí)筆記1 MySql簡(jiǎn)單上手
- Mysql源碼學(xué)習(xí)筆記 偷窺線程
- 一千行的MySQL學(xué)習(xí)筆記匯總
相關(guān)文章
win10下mysql 8.0.18 安裝配置方法圖文教程(windows版)
這篇文章主要介紹了windows版的mysql 8.0.18 安裝配置方法圖文教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Windows8下mysql 5.6.15 安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了Windows8下mysql 5.6.15 安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09MySQL中導(dǎo)出用戶權(quán)限設(shè)置的腳本分享
這篇文章主要介紹了MySQL中導(dǎo)出用戶權(quán)限設(shè)置的腳本分享,本文通過(guò)導(dǎo)出mysql.user表中數(shù)據(jù)實(shí)現(xiàn)導(dǎo)出權(quán)限設(shè)置,需要的朋友可以參考下2014-10-10mysql之delete刪除記錄后數(shù)據(jù)庫(kù)大小不變
這篇文章主要介紹了mysql之delete刪除記錄后數(shù)據(jù)庫(kù)大小不變的相關(guān)資料,需要的朋友可以參考下2016-06-06IDEA連接mysql又報(bào)錯(cuò)!Server returns invalid timezone. Go to tab an
這篇文章主要介紹了IDEA連接mysql又報(bào)錯(cuò)!Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' prope問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-05-05