1. 程式人生 > >MySQL單表查詢多表查詢

MySQL單表查詢多表查詢

(1)查詢欄位,可以指定select *或者某些欄位名
(2)查詢記錄,通過where限定一個條件。如取出dept_no=20的e_name和e_job資訊
obj.selectSql('select e_name,e_job from employee where dept_no=20')
(3)and or語句,and表示要滿足多個條件同時成立,or表示滿足其中任意一個成立,使用這兩種語法可以把每個條件用小括號括起來,避免邏輯混亂
(4)in查詢,對where條件的擴充套件。指定條件等於in中的某一項,如選出depy=20或者30的所有資訊
obj.selectSql('select * from employee where dept_no in (20,30)')
如果不適用in 那麼需要指定dept_no=20 or dept_no=30  太麻煩,not in同理
(5)between and查詢,也是對where的擴充套件。如選出e_no在1003到1007中間的資訊
obj.selectSql('select * from employee where e_no between 1003 and 1007')
也可用e_no>=1003 and e_no<=1007實現,一樣過於麻煩
(6)查詢空值is null,同樣是對where條件的擴充套件,如果是空值,則條件成立輸出該條記錄,is not null同理
(7)like語法:_和%都是萬能字元,_只能代表一個字元,%可以代表任意多個。如選出e_name中有s開頭的記錄
obj.selectSql('select * from employee where e_name like "s%"')
(8)distinct去重語句,這個語句一般只用於單個欄位查詢才有用(第七章專家點評第一個詳解)。
(9)order by語句:order by可以完成單列排序,多列排序(排序順序,先來後到),指定方向的排序。如檢視employee表中所有資訊,先對e_job降序排列,再對e_salary升序排列
obj.selectSql('select * from employee order by e_job desc,e_salary')
先對e_job降序排序然後對e_salary升序排列,可以試試先排e_salary後排e_job的結果
(10)Group by語句:可以完成單欄位分組,多欄位分組(注意分組順序),having過濾,with rollup
單欄位分組,查出所有的職位e_job及職位數量
obj.selectSql('select e_job,count(*) from employee group by e_job')
多欄位分組,查出所有的職位e_job中不同工作部門dept_no的數量
obj.selectSql('select e_job,dept_no,count(*) from employee group by e_job,dept_no')
having用法,查出所有的職位e_job中不同工作部門dept_no的數量大於1的記錄
obj.selectSql('select e_job,dept_no,count(*) from employee group by e_job,dept_no having count(*)>1')
having和where都是過濾語句,區別在於:having在資料分組之後過濾,而where是在資料分組之前過濾,且where過濾掉的資料不會再參與分組。所以where語句應放在Group語句前
obj.selectSql('select e_job,dept_no,count(*) from employee where dept_no=20 group by e_job')
with rollup就是在所有查詢出來的分組記錄後再新增一個記錄,統計記錄數量
obj.selectSql('select e_job,dept_no,count(*) from employee where dept_no=20 group by e_job with rollup')
order by和group by的結合使用,先分組,後對每個分組進行排序,按順序寫就好
(11)limit語句 限制查詢數量。如從所有資訊的第3條開始查詢,查詢4條記錄,第一個引數不寫預設為0
obj.selectSql('select * from employee limit 2,4')
(12)union語句,將查詢相同欄位不同條件的結果合併在一起並且去除掉重複的查詢結果,union all不去重但效率比union高
obj.selectSql('select * from employee where dept_no=20 union select * from employee where e_salary>2000')
union可以使用or語句代替
obj.selectSql('select * from employee where dept_no=20 or e_salary>2000')
二:多表查詢  使用dept和employee表
(i):連線查詢
 (1)內連線:如查詢e_gender為f的員工的所有資訊(包括dept和employee的所有資訊)
內連線就是對兩個表的交集做一些條件限制
語法1:
obj.selectSql('select * from dept,employee where dept.d_no=employee.dept_no and employee.e_gender="f"')
語法2:
obj.selectSql('select * from dept inner join employee on dept.d_no=employee.dept_no and employee.e_gender="f"')
語法2是sql標準語法,一般使用這種語法
 (2)左右連線
左連線:左表說我最厲害,必須保持我的完整,右表你過濾過之後要向我看齊,我有你沒有的你趕快和我看齊補空值,你有我沒有的趕快扔了
如以左連線查詢dept和employee中e_gender為f的員工資訊
obj.selectSql('select * from dept left join employee on dept.d_no=employee.dept_no and employee.e_gender="f"')
右連線:如以右連線查詢dept和employee中e_gender為f的員工資訊
obj.selectSql('select * from dept right join employee on dept.d_no=employee.dept_no and employee.e_gender="f"')
(ii)子查詢 (能用連線查詢代替儘量不使用子查詢,效率低)
 (1)帶有any,some的子查詢,關鍵字接在一個比較操作符後,表示若與子查詢返回的其中一個值比較為true則返回true
 (2)帶有all的子查詢,關鍵字接在一個比較操作符後,表示若與子查詢返回的所有值比較均為true才返回true
 (3)exists關鍵字後跟一個子查詢,如果查詢到東西則返回true,外查詢開始執行
 (4)in關鍵字子查詢返回一個序列,功能與單表查詢中的in語句相同
三:其他
  (1)函式:常用的有count(),max(),avg(),min(),sum()
  (2)as語法:as語法給欄位或者表改名,語法:欄位名或列名 as 新欄位名或列名
  (3)正則語法查詢:使用regexp關鍵字,語法和python正則語法相同,可以和like做下對比
obj.selectSql('select * from employee where e_name regexp "^s"')
obj.selectSql('select * from employee where e_name like "s%"')
  (4)group by之後如果想檢視某個分組內的某個欄位內容可以用group_concat
如  檢視所有職位的工作人員名字
obj.selectSql('select e_job,count(*) as total,group_concat(e_name) as name from employee group by e_job')
如果不使用group_concat 直接檢視e_name 返回的是該分組內其中一個人的名字