python 學習_第四模塊 mysql 單表查詢python
select distinct 字段1,字段2,字段3 from 庫.表 where 條件 group by 分組條件 having 過濾 order by 排序字段 limit n;
1.找到表:from 2.拿着where指定的約束條件,去文件/表中取出一條條記錄 3.將取出的一條條記錄進行分組group by,若是沒有group by,則總體做爲一組 4.將分組的結果進行having過濾 5.執行select 6.去重 7.將結果按條件排序:order by 8.限制結果的顯示條數
一 簡單查詢 mysql
1 distinct 去重 正則表達式
select distinct post from employee;
2 定義顯示格式sql
CONCAT() 函數用於鏈接字符串 mysql> select concat('姓名:' ,name,' 年薪:',salary*12) as annual_salary from employee; +------------------------------------+ | annual_salary | +------------------------------------+ | 姓名:egon 年薪:87603.96 | | 姓名:alex 年薪:12000003.72 | | 姓名:wupeiqi 年薪:99600.00 | | 姓名:yuanhao 年薪:42000.00 |
CONCAT_WS() 第一個參數爲分隔符 mysql> select concat_ws(':',name,salary*12) as Annual_salary from employee; +----------------------+ | Annual_salary | +----------------------+ | egon:87603.96 | | alex:12000003.72 | | wupeiqi:99600.00 |
mysql> select name,salary*12 annual_salary from employee; +------------+---------------+ | name | annual_salary | +------------+---------------+ | egon | 87603.96 | | alex | 12000003.72 | mysql> select concat("姓名:",name," 性別:" , sex)as info, concat( salary*12) as annual_salary from employee; +---------------------------------+---------------+ | info | annual_salary | +---------------------------------+---------------+ | 姓名:egon 性別:male | 87603.96 | | 姓名:alex 性別:male | 12000003.72 | | 姓名:wupeiqi 性別:male | 99600.00 |
二 where 約束函數
比較運算符 : > <> = <= >= !=
between 80 and 100
in (10,20,30)
like "tes%"
邏輯運算符 and or not post
in 學習
select * from employee where age in (73,78,28); select * from employee where post_comment is NULL; select * from employee where post_comment is NOT NULL; select * from employee where age between 20 and 30;
不在20 到30之間spa
select * from employee where age not between 20 and 30;
likecode
select * from employee where name like "jin%";
1. 查看崗位是teacher的員工姓名、年齡 select name,age from employee where post="teacher"; 2. 查看崗位是teacher且年齡大於30歲的員工姓名、年齡 select name,age from employee where post="teacher" and age >30; 3. 查看崗位是teacher且薪資在9000-10000範圍內的員工姓名、年齡、薪資 select name,age,salary from employee where salary between 9000 and 10000; 4. 查看崗位描述不爲NULL的員工信息 select * from employee where post_comment is NOT NULL; 5. 查看崗位是teacher且薪資是10000或9000或30000的員工姓名、年齡、薪資 select name,age,salary from employee where post="teacher" and salary not in (10000,3000,30000); 6. 查看崗位是teacher且薪資不是10000或9000或30000的員工姓名、年齡、薪資 select name,age,salary from employee where post="teacher" and salary in (10000,3000,30000); 7. 查看崗位是teacher且名字是jin開頭的員工姓名、年薪 select name,concat(salary *12)as annual_salary from employee where name like "jin%";
三 分組查詢:GROUP BYregexp
5.6 設置 ONLY_FULL_GROUP_BY
set global sql_mode='ONLY_FULL_GROUP_BY';
退出mysql 從新登陸
group_concat
1. 查詢崗位名以及崗位包含的全部員工名字 select post ,group_concat(name) from employee group by post; 2. 查詢崗位名以及各崗位內包含的員工個數 select post,count(id) from employee group by post; 3. 查詢公司內男員工和女員工的個數 select sex,count(id) from employee group by sex; 4. 查詢崗位名以及各崗位的平均薪資 select post,avg(salary) from employee group by post; 5. 查詢崗位名以及各崗位的最高薪資 select post,max(salary) from employee group by post; 6. 查詢崗位名以及各崗位的最低薪資 select post,min(salary) from employee group by post; 7. 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資 select sex,avg(salary) from employee group by sex;
四 HAVING過濾
執行優先級從高到低:where > group by > having
1. 查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數 select post,group_concat(name),count(id) from employee group by post having count(id) <2; 2. 查詢各崗位平均薪資大於10000的崗位名、平均工資 select post,avg(salary) from employee group by post having avg(salary) > 10000; 3. 查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資 select post,avg(salary) from employee group by post having avg(salary) > 10000 and avg(salary)< 20000;
五 查詢排序:ORDER BY
asc 升序 select * from employee order by age asc; desc 降序 select * from employee order by age desc;
六 限制查詢的記錄數:LIMIT
從第0位開始 5位 select * from employee limit 0,5;
七 使用正則表達式查詢
select * from employee where name like "jin%"; select * from employee where name regexp "^jin"; jin 開頭 g結尾 select * from employee where name regexp "^jin.*g$";