MySQL【七】單表查詢

MySQL單表查詢

 

where條件

比較運算符

> < >= <= <> !=

select 字段 from 表 where 條件
    select * from 表名 where 字段>範圍;  

between

between a and b  # 查詢a,b之間的全部內容(雙閉合)
    select * from 表名 where 字段  between a and b;

in

in(a,b,c) # 查詢值爲a或者b或者c的全部內容
    select * from 表名 where 字段 in(a,b);

like

select * from 表名 where 字段 like '匹配內容%';
            
%是一個通配符,表示任意長度的任意內容
    select *  from 表名 where 字段 like '程%';
_也是一個通配符,表示一個長度的任意內容
    select * from 表名 where 字段 like '程咬_'

邏輯運算符 and or not

複製代碼
select * from 表名 where 字段 and 字段
    select * from employee where age=18 and salary<10000;

select * from 表名 where 字段 or 字段
    select * from employee where age=18 or salary<10000;

select * from 表名 where 字段 not in 字段
    select * from employee where post not in ('teacher');
複製代碼

身份運算符 is null/ is not null

select * from 表名 where 字段 is null; 查詢這個字段裏全部爲空的值
    select * from employee where post_comment is null;

select * from 表名 where 字段 is not null; 查詢這個字段中全部不爲空的值
    select * from employee where post is not null;

正則匹配

全部人的身份證號,匹配全部身份證號是15位的居民  ^\d{15}$
    select 字段 from 表 where age regex '^\d{15}$';

group系列

group by分組

select * from 表名 group by 字段;
    select * from employee group by post;

group_concat 查看組內的名字

select group_concat(emp_name) from employee group by sex;

group_count  計數

select sex,count(emp_name) from employee group by sex;

having  過濾

對分組進行條件過濾 老是和group by 連用,where中不能出現聚合函數,因此和聚合函數有關的條件篩選也只能用having
老是對分組以後的結果進行一個條件篩選的時候用havinghtml

查各個崗位的員工個數
select post,count(id) from employee group by post having count(id) <2

查詢各崗位平均薪資大於10000的崗位名、平均工資
select  post,avg(salary) from employee group by post having avg(salary) > 10000;
先將崗位分組而後分別對每一個崗位的薪資求平均值,而後在篩選平均薪資大於10000的

order by 排序

# 默認從小到大排序  升序
    select * from employee  order by age;
# 從大到小排序 desc 降序
    select * from employee  order by age desc;

聚合函數web

先from找到表
再用where的條件約束去表中取出記錄
而後進行分組group by,沒有分組則默認一組
而後進行聚合
最後select出結果sql

複製代碼
示例:
    select count(*) from employee;
    select count(*) from employee where depart_id=1;
    select max(salary) from employee;
    select min(salary) from employee;
    select avg(salary) from employee;
    select sum(salary) from  employee;
    select sum(salary) form employee WHERE depart_id=3;
複製代碼

limit

複製代碼
limit 取前n個或者web開發中作分頁功能

# 顯示前n條 limit n
# 從第m+1條開始,顯示n條   limit m,n
        select * from employee  order by age limit 1,6;
# 從第m+1條開始,顯示n條 limit n offset m
        select * from employee  order by age limit 6 offset 10;
複製代碼
複製代碼
=========limit:限制打印幾條=========
1.select * from employee limit 3;#打印前三條
2.像這樣表示的:指的是從哪開始,日後取幾條 (這樣的操做通常用來分頁)
select * from employee limit 0,3;
select * from employee limit 3,4;
select * from employee limit 6,3;
select * from employee limit 9,3;
3.select * from employee order by id desc limit 3; #查看後三條
複製代碼

練習:查詢各崗位平均薪資大於10000的崗位名、平均工資,結果按平均薪資升序排列;函數

select post,avg(salary) as avg_salary from employee group by post having avg(salary)>10000 order by avg_salary

sql的解析順序post

select distinct 字段 from 表 where 條件 group by 分組 having 過濾條件 order by 排序 limit n;

關鍵字的執行優先級url

複製代碼
from
where
group by
having
select
distinct
order by
limit
複製代碼
相關文章
相關標籤/搜索