Mysql基本查詢語句及方法


在mysql中 查詢一張表中的記錄的時候 

  書寫順序是: select * from 表名 where name='long' and age ='18';

但是mysql中的執行順序是 

  from  后面加表名 確定你是那張表

  where 后面是條件 通過條件 來篩選這表的內容

  select 后面是 你where篩選出的數據中的 某些字段    * 是所有字段

  # 查詢語句執行的結果也是一張表,可以看成虛擬表

我們創建一張 emp的員工表

當我們的字段特別多的時候  結果的排版可能會出現凌亂現象 我們可以在查詢語句末尾 加上\G來規范查詢結果

select * from 表名\G;

當我們遇到一個需求時 怎么來分析? 例如 

1.查詢id大於等於3小於等於6的數據

   給你展示下實際操作  1.先確定 來自哪一張表  from emp   2. 篩選條件 where id >= 3 and id <=6;   3.select  *

   select * from emp where id >= 3 and id <= 6;
    select * from emp where id between 3 and 6;  between 等價於id >= 3 and id <= 6

 

2.查詢薪資是20000或者18000或者17000的數據

    select id,name from emp where salary = 20000 or salary = 18000 or salary = 17000;
    select id,name from emp where salary in (20000,18000,17000);

 

3.查詢員工姓名中包含o字母的員工姓名和薪資

模糊匹配  % 匹配多個任意字符  _ 匹配 一個任意字符

select name,salary from emp where name like '%o%';

 

4.查詢員工姓名是由四個字符組成的員工姓名與其薪資 

select name, salary from emp where length(name) =4;
select name ,salary from emp where name like "____"

 

5.查詢id小於3或者大於6的數據

select * from emp where id<3 or id >6;
select * from emp where id not between 3 and 6; 

 

6.查詢薪資不在20000,18000,17000范圍的數據

select * from emp  where  salary not in (20000,17000,18000);

 

7.查詢崗位描述為空的員工名與崗位名   針對null判斷的時候只能用is 不能用=

select name ,post from emp where post_comment is null;

 

MySQL對大小寫不敏感  平時寫的時候大小寫都可以

group by 分組

select * from emp group by post;  # 按照部門分組

分組后 應該做到 最小單位是 組 ,而不應該是 展示 組內的單個數據信息 

 向上面那樣 他會直接給你 打印出來而沒有給你報錯 說明你的嚴格模沒有設置

show variables '%mode%'; # 找到嚴格模式所在的地方

set session # 臨時有效 
set global  # 永久有效

set global sql_mode= 'strict_trans_tables' # 設置字符類型的自動截取
set global sql_mode="strict_trans_tables,pad_char_to_full_length" #char 取出時 取消自動去空格

set global sql_mode='strict_trans_tables,only_full_group_by' # 設置分組后   最小單位是組     

 

此時你如果還使用 select name from emp group by post; 就會報錯 #ERROR 1055 (42000): 'day37.emp.name' isn't in GROUP BY

selest 后應該接的是 你分組的字段名

 

聚合函數(max, min ,sum,count, avg)  聚集到一起合成為一個結果

mysql中 分組之后 只能拿到分組的字段信息 無法直接 獲取其他字段的信息 但是 你可以通過其他方法來間接的獲取(聚合函數)

獲取每個部門的最高工資 

   需求是 每一個部門 說明有分組 所以 先分組 在使用聚合函數來取值

select post ,max(salary) from emp group by post;

 

每個部門的最低工資

select post,min(salary) from emp group by post;
取的時候還可以給字段取別名
select post,min(salary) as '最小' from emp group by post;

每個部門的平均工資

select post,avg(salary) from emp group by post;

 

每個部門的工資總和

select post,sum(salary) from emp group by post;

 

每個部門的人數

    select post,count(age) from emp group by post;
    select post,count(salary) from emp group by post;
    select post,count(id) from emp group by post;
    select post,count(post_comment) from emp group by post;

 

 在統計分組內個數的時候 填寫任意非空字段都可以完成計數,推薦使用能夠唯一標識數據的字段  比如id字段

      聚合函數會自動將每一個分組內的單個數據做想要的計算,無需你考慮

group_concat

查詢分組之后的部門名稱和每個部門下所有的學生姓

select post, group_concat(name) from emp group by post;

select post,group_concat('hahha',name) from emp group by post;

 還可以拼接

 

group_concat()能夠拿到分組后每一個數據指定字段(可以是多個)對應的值

 concat就是用來幫你拼接數據

group_concat(分組之后用)

concat(不分組時用)

 查詢每個員工的年薪

select name,salary*12 from emp;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM