天天看點

MYSQL資料檢索總結

用到的表是employee,建表語句如下(也可以自己建一個):

create table employee(
number varchar() primary key not null,
name varchar(),
age int,
salary decimal(,)
);
           

插入的資料如下:

insert into employee(number,name,age,salary)
values('DEV001','Tom',,),('DEV002','Jerry',,),
('SALE001','John',,),('SALE002','Kerry',,),
('SALE003','Stone',,),('HR001','Jane',,),
('HR002','Tina',,),('IT001','Smith',,);
           

資料檢索總結如下:

1.為列取别名

定義格式:列名 AS 别名 (AS可以省略)

    例如:select FNumber as number from employee;
        或 select FNumber number from employee;
           

2.按條件過濾

where 條件
           

3.資料彙總

常用的聚合函數(參數都是字段名):

        1).max 計算字段最大值

            例子:select max(salary) as max_salary from
                 employee where age > 25;

        2).min 計算字段最小值

            例子:select avg(age) as avg_age from employee where salary > 3800;

                 select min(salary),max(salary) from employee;

        3).avg 計算字段平均值

            例子:select avg(age) as avg_age from employee where salary > 3800;

        4).sum 計算字段合計值

            例子:select sum(salary) as total_salary from employee;

        5).count統計資料條數

            這個函數有兩種用法,一個是count(*)統計的是結果集的總條數,一個是count(字段名)是統計結果集中該字段名不為空值的條數.
           

4.排序

文法:order by 字段名 ASC (升序,這是預設的可省略)
         order by 字段名 DESC (降序)

    例子:select * from employee order by age;

         select * from employee order by age desc;

    可以指定多個排序列
    規則是:資料庫系統會按照優先級進行處理,先按照第一條排序規則進行排序,如果第一個排序規則無法區分兩條記錄的順序則按照第二條排序規則進行排序,依次類推.
    還要注意的是,order by語句必須在where語句之後。

    例子:select * from employee order by age desc,salary desc;
           

5.進階資料過濾

通配符過濾需要使用like關鍵字
    1).單字元比對

        利用_表示一個任意字元,比如a_b,可以比對到acb adb等等._還可以出現多次,__a__b__,這裡一共有6個任意字元.

        例子:select * from employee where name like '_erry';

             select * from employee where name like '__n_';

    2).多字元比對

        利用%可以比對任意多個字元(0或N個)
        單字元和多字元比對可以一起使用.
        例子:select * from employee where name like 'T%';

             select * from employee where name like '%n_';

    3).集合比對

        這個功能隻在MSSQLServer上支援,但是可以利用通配符加邏輯符實作

        首先集合比對的用法為:[SJ] 可以比對 S 或 J

                             [^SJ] 可以比對非S 或 非 J

        例子:select * from employee where name like '[SJ]%';
        等價于 
        select * from employe where name like 'S%' or name like 'J%';

        select * from employee where name like '[^SJ]%'
        等價于 
        select * from employee where not (name like 'S%')
        and not(name like 'J%');

    4).空值檢測

        不能使用普通的等于運算符進行判斷,而要使用is null關鍵字,不為空則使用 is not null

        用法:待檢測字段名 is null

        例子:select * from employee where name is null;
             select * from employee where name is not null;         

    5).反義運算符

        !表示反義,例如 != 不等于 !< 不小于 !>不大于
        注意隻有MSSQLServer和DB2資料庫上支援,可以用同義詞變通,例如不小于就是大于等于,SQL提供了通用的不等于運算符<>.

        例子:select * from employee where age!=22 and salary!<2000;  
        等價于
        select * from employee where age<>22 and salary >=2000;      
        等價于
        select * from employee where not(age=22) and not(salary <2000);      

    6).多值檢測

        可以使用or語句來連接配接多個等于判斷或者使用in語句

        例子:select age,number,name from employee where
             age=23 or age=25 or age=28;

             select age,number,name from employee where
             age in(23,25,28);

    7).範圍值檢測

        可以使用> >= < <=來實作,也可以使用SQL提供從範圍檢測語句實作
        字段名 bettween 左值範圍 and 右值範圍 (閉區間)
        例子:select * from employee where age>=23 and age<=27;
        等價于
        select * from employee where age between 23 and 27;

    8).數組分組

        資料分組用來将資料分為多個邏輯組,進而可以對每個組進行聚合運算。且分組語句必須和聚合函數一起使用

        用法:group by 分組字段

        group by 子句中可以指定多個列,然後資料庫進行逐層分組。意思是先按照第一個字段分組,然後再對每個組再進行第二個字段的分組,進而實作組中組.

        例子:将年齡分組,統計每個年齡組的人數和平均工資
             select age,count(*) as countofthisage,avg(salary) as avg_salary  from employee group by age;


        還可以用having語句進行組的過濾

        例子:select age,count(*) from employee group by age having count(*)>1
        意思是一個組的記錄要大于1

    9).限制結果集行數

        limit 首行行号,要傳回的結果集最大數

        例子:select * from employee order by salary desc limit 2,5;

    10).抑制資料重複

        distinct關鍵字是用來進行重複資料抑制的,它是對整個結果集進行資料重複抑制的,
        而不是針對每一個列

        例子:select distinct depatment from employee;

    11).常量字段

        例子:select 'CowNew集團' as CopanyName,name age,subcompany from employee;

        CowNew集團就是常量字段,它不是真實存在的.

    12).字段間計算

        例子:select number,name,age*salary from employee;

        字段間可以做運算,不管是常量字段還是普通字段,兩兩之間都能做運算。

    13).資料處理函數

        除了聚合函數之外,還有數學函數、日期處理函數、字元串處理函數.
        常用的有length、substring、sin、abs
        例子:select name,length(name) as nameLength from    employee where name is not null;

             select name,substring(name,2,3) from employee where name is not null;

             select name,age,sin(age),abs(sin(age)) from employee;

    14).字元串拼接

        字元串拼接要使用concat,如果使用“+”的話,SQL會自動把兩邊轉換成數字進行相加運算,轉換失敗則為0.

        例子:select concat('工号為:',number,'的員工的幸福指數:',salary/(age-21)) from employee;

        concat支援隻有一個參數的用法,這時concat可以看作是一個将這個參數值嘗試轉化為字元串類型值的函數.

        concat_ws函數可以在待拼接的字元串之間加入指定的分隔符,它的第一個參數為采用的分隔符,而剩下的參數則為待拼接的字元串.

        例子:select concat_ws(',',number,age,depatment,salary) from employee;

    15).聯合結果集

        使用union運算符來将兩個或多個查詢結果集聯合為一個結果集中
        基本原則:每個結果集必須有相同的列數,每個結果集的列必須類型相同.

        例子:select number,name,age from employee union
             select idCardNumber,name,age from tempemployee;

        預設情況下,union運算符合并了兩個查詢結果集,其中完全重複的資料行被合并為了一條。使用union all可以避免這種情況.

        例子:select name,age from employee union all
             select name,age from tempemployee;