1. 程式人生 > >數據庫之單表查詢

數據庫之單表查詢

年齡 tween 員工 分頁 lar 取出 arc windows mit

select * from emp where name regexp ^jin.*(n|g)$;

技術分享圖片

找到表:from
2.拿著where指定的約束條件,去文件/表中取出一條條記錄
3.將取出的一條條記錄進行分組group by,如果沒有group by,則整體作為一組
4.將分組的結果進行having過濾
5.執行select
6.去重
7.將結果按條件排序:order by
8.限制結果的顯示條數
詳細:http://www.cnblogs.com/linhaifeng/articles/7372774.html

技術分享圖片

準備表和記錄
company.employee
    員工id      id                  int
    姓名        emp_name            varchar
    性別        sex                 enum
    年齡        age                 int
    入職日期     hire_date           date
    崗位        post                varchar
    職位描述     post_comment        varchar
    薪水        salary              double
    辦公室       office              int
    部門編號     depart_id           int



#創建表 create table emp( id int not null unique auto_increment, name varchar(20) not null, sex enum(male,female) not null default male, #大部分是男的 age int(3) unsigned not null default 28, hire_date date not null, post varchar(50), post_comment varchar(100), salary double(15,2), office int, #一個部門一個屋子 depart_id int );
#查看表結構 mysql> desc employee; +--------------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+-----------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | sex | enum(
male,female) | NO | | male | | | age | int(3) unsigned | NO | | 28 | | | hire_date | date | NO | | NULL | | | post | varchar(50) | YES | | NULL | | | post_comment | varchar(100) | YES | | NULL | | | salary | double(15,2) | YES | | NULL | | | office | int(11) | YES | | NULL | | | depart_id | int(11) | YES | | NULL | | +--------------+-----------------------+------+-----+---------+----------------+ #插入記錄 #三個部門:教學,銷售,運營 insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values (egon,male,18,20170301,老男孩駐沙河辦事處外交大使,7300.33,401,1), #以下是教學部 (alex,male,78,20150302,teacher,1000000.31,401,1), (wupeiqi,male,81,20130305,teacher,8300,401,1), (yuanhao,male,73,20140701,teacher,3500,401,1), (liwenzhou,male,28,20121101,teacher,2100,401,1), (jingliyang,female,18,20110211,teacher,9000,401,1), (jinxin,male,18,19000301,teacher,30000,401,1), (成龍,male,48,20101111,teacher,10000,401,1), (歪歪,female,48,20150311,sale,3000.13,402,2),#以下是銷售部門 (丫丫,female,38,20101101,sale,2000.35,402,2), (丁丁,female,18,20110312,sale,1000.37,402,2), (星星,female,18,20160513,sale,3000.29,402,2), (格格,female,28,20170127,sale,4000.33,402,2), (張野,male,28,20160311,operation,10000.13,403,3), #以下是運營部門 (程咬金,male,18,19970312,operation,20000,403,3), (程咬銀,female,18,20130311,operation,19000,403,3), (程咬銅,male,18,20150411,operation,18000,403,3), (程咬鐵,female,18,20140512,operation,17000,403,3) ; #ps:如果在windows系統中,插入中文字符,select的結果為空白,可以將所有字符編碼統一設置成gbk

技術分享圖片

技術分享圖片

小練習:
    select id,name from db39.emp where id >= 3 and id <= 6;
    select *  from db39.emp where id between 3 and 6;


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


    要求:查詢員工姓名中包含i字母的員工姓名與其薪資
    select name,salary from db39.emp where name like %i%

    要求:查詢員工姓名是由四個字符組成的的員工姓名與其薪資
    select name,salary from db39.emp where name like ____;
    select name,salary from db39.emp where char_length(name) = 4;



    select *  from db39.emp where id not between 3 and 6;
    select * from emp where salary not in (20000,18000,17000);

    要求:查詢崗位描述為空的員工名與崗位名
    select name,post from db39.emp where post_comment is NULL;
    select name,post from db39.emp where post_comment is not NULL;

技術分享圖片

   一 什麽是分組?為什麽要分組?
    #1、首先明確一點:分組發生在where之後,即分組是基於where之後得到的記錄而進行的
    #2、分組指的是:將所有記錄按照某個相同字段進行歸類,比如針對員工信息表的職位分組,或者按照性別進行分組等
    #3、為何要分組呢?
        取每個部門的最高工資
        取每個部門的員工數
        取男人數和女人數

    小竅門:‘每’這個字後面的字段,就是我們分組的依據
    #4、大前提:
        可以按照任意字段分組,但是分組完畢後,比如group by post,只能查看post字段,如果想查看組內信息,需要借助於聚合函數

   二,only_full_group_by  (聚合函數)
   三,group by
    #設置sql_mode為only_full_group_by,意味著以後但凡分組,只能取到分組的依據默認
    mysql> set global sql_mode="strict_trans_tables,only_full_group_by";

    #每個部門的最高工資
    select post,max(salary) from emp group by post;
    select post,min(salary) 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(id) from emp group by post;

技術分享圖片

select post,group_concat(name) from emp group by post;
    select post,group_concat(name,"_SB") from emp group by post;
    select post,group_concat(name,": ",salary) from emp group by post;
    select post,group_concat(salary) from emp group by post;

技術分享圖片

技術分享圖片

技術分享圖片

  select name as 姓名,salary as 薪資 from emp;
    mysql> select emp.id,emp.name from emp as t1; # 報錯
    mysql> select t1.id,t1.name from emp as t1;

技術分享圖片

技術分享圖片

技術分享圖片

  having的語法格式與where一模一樣,只不過having是在分組之後進行的進一步過濾
    即where不能用聚合函數,而having是可以用聚合函數,這也是他們倆最大的區別

    1、統計各部門年齡在30歲以上的員工平均工資,並且保留平均工資大於10000的部門
    select post,avg(salary) from emp
            where age >= 30
            group by post
            having avg(salary) > 10000;

    #強調:having必須在group by後面使用
    select * from emp
            having avg(salary) > 10000;

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

select * from emp order by salary asc; #默認升序排
select * from emp order by salary desc; #降序排

select * from emp order by age desc; #降序排

select * from emp order by age desc,salary asc; #先按照age降序排,再按照薪資升序排



# 統計各部門年齡在10歲以上的員工平均工資,並且保留平均工資大於1000的部門,
然後對平均工資進行排序

select post,avg(salary) from emp
    where age > 10
    group by post
    having avg(salary) > 1000
    order by avg(salary)
    ;

技術分享圖片

select * from emp limit 3;

select * from emp order by salary desc limit 1;



# 分頁顯示
select * from emp limit 0,5;
select * from emp limit 5,5;

技術分享圖片

select * from emp where name regexp ^jin.*(n|g)$;

數據庫之單表查詢