主頁 > 後端開發 > 四十七、MySQL資料庫4

四十七、MySQL資料庫4

2021-09-06 06:29:44 後端開發

今日內容概要

  • 如何查詢表
    • 前期表準備
    • 幾個重要關鍵字的執行順序
    • where篩選條件
    • group by 分組
    • 分組注意事項
    • having分組之后的篩選條件
    • distinct去重
    • order by排序
    • limit限制展示條數
    • 正則
  • 聯表操作理論
    • 前期表準備
    • 表查詢
    • 子查詢
    • 總結

今日內容詳細

如何查詢表

  前期表準備

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
);    

#插入資料
#三個部門:教學部,銷售部,運營部

insert into 
emp(name,sex,age,hire_date,post,salary,office,depart_id) values
('jason','male',18,'20170301','張江第一帥形象',7300.33,401,1),
('tom','male',78,'20150302','teacher',1000000.31,401,1),
('kevin','male',81,'20130305','teacher',8300,401,1),
('tony','male',73,'20140701','teacher',3500,401,1),
('owen','male',28,'20121101','teacher',2100,401,1),
('jack','female',18,'20110211','teacher',9000,401,1),
('jenny','male',18,'19000301','teacher',30000,401,1),
('sank','male',48,'20101111','teacher',10000,401,1),
('哈哈','female',48,'20150311','sale',3000.13,402,2),    #以下是銷售部門
('呵呵','female',48,'20101101','sale',2000.35,402,2),
('西西','female',38,'20110312','sale',1000.37,402,2),
('樂樂','female',18,'20160513','sale',3000.29,402,2),
('啦啦','female',18,'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',18000,403,3),
('程咬銅','male',18,'20150411','operation',19000,403,3),
('程咬鐵','female',18,'20140512','operation',17000,403,3);

mysql> select * from emp;
+----+-----------+--------+-----+------------+-----------------------+--------------+------------+--------+-----------+
| id | name      | sex    | age | hire_date  | post                  | post_comment | salary     | office | depart_id |
+----+-----------+--------+-----+------------+-----------------------+--------------+------------+--------+-----------+
|  1 | jason     | male   |  18 | 2017-03-01 | 張江第一帥形象        | NULL         |    7300.33 |    401 |         1 |
|  2 | tom       | male   |  78 | 2015-03-02 | teacher               | NULL         | 1000000.31 |    401 |         1 |
|  3 | kevin     | male   |  81 | 2013-03-05 | teacher               | NULL         |    8300.00 |    401 |         1 |
|  4 | tony      | male   |  73 | 2014-07-01 | teacher               | NULL         |    3500.00 |    401 |         1 |
|  5 | owen      | male   |  28 | 2012-11-01 | teacher               | NULL         |    2100.00 |    401 |         1 |
|  6 | jack      | female |  18 | 2011-02-11 | teacher               | NULL         |    9000.00 |    401 |         1 |
|  7 | jenny     | male   |  18 | 1900-03-01 | teacher               | NULL         |   30000.00 |    401 |         1 |
|  8 | sank      | male   |  48 | 2010-11-11 | teacher               | NULL         |   10000.00 |    401 |         1 |
|  9 | 哈哈      | female |  48 | 2015-03-11 | sale                  | NULL         |    3000.13 |    402 |         2 |
| 10 | 呵呵      | female |  48 | 2010-11-01 | sale                  | NULL         |    2000.35 |    402 |         2 |
| 11 | 西西      | female |  38 | 2011-03-12 | sale                  | NULL         |    1000.37 |    402 |         2 |
| 12 | 樂樂      | female |  18 | 2016-05-13 | sale                  | NULL         |    3000.29 |    402 |         2 |
| 13 | 啦啦      | female |  18 | 2017-01-27 | sale                  | NULL         |    4000.33 |    402 |         2 |
| 14 | 僧龍      | male   |  28 | 2016-03-11 | operation             | NULL         |   10000.13 |    403 |         3 |
| 15 | 程咬金    | male   |  18 | 1997-03-12 | operation             | NULL         |   20000.00 |    403 |         3 |
| 16 | 程咬銀    | female |  18 | 2013-03-11 | operation             | NULL         |   18000.00 |    403 |         3 |
| 17 | 程咬銅    | male   |  18 | 2015-04-11 | operation             | NULL         |   19000.00 |    403 |         3 |
| 18 | 程咬鐵    | female |  18 | 2014-05-12 | operation             | NULL         |   17000.00 |    403 |         3 |
+----+-----------+--------+-----+------------+-----------------------+--------------+------------+--------+-----------+
18 rows in set (0.00 sec)

當表的欄位很多的時候,命令視窗不夠寬,感覺資料錯亂,怎么辦???
只需要在select * from emp后面加上 \G
即:select * from emp \G;

個別同學的電腦在插入中文的時候還是會出現亂碼或者空白的現象
你可以將字符編碼統一設定成gbk

select * from emp \G;

mysql> select * from emp \G;
*************************** 1. row ***************************
          id: 1
        name: jason
         sex: male
         age: 18
   hire_date: 2017-03-01
        post: 張江第一帥形象
post_comment: NULL
      salary: 7300.33
      office: 401
   depart_id: 1
*************************** 2. row ***************************
          id: 2
        name: tom
         sex: male
         age: 78
   hire_date: 2015-03-02
        post: teacher
post_comment: NULL
      salary: 1000000.31
      office: 401
   depart_id: 1
*************************** 3. row ***************************
          id: 3
        name: kevin
         sex: male
         age: 81
   hire_date: 2013-03-05
        post: teacher
post_comment: NULL
      salary: 8300.00
      office: 401
   depart_id: 1
*************************** 4. row ***************************
          id: 4
        name: tony
         sex: male
         age: 73
   hire_date: 2014-07-01
        post: teacher
post_comment: NULL
      salary: 3500.00
      office: 401
   depart_id: 1
*************************** 5. row ***************************
          id: 5
        name: owen
         sex: male
         age: 28
   hire_date: 2012-11-01
        post: teacher
post_comment: NULL
      salary: 2100.00
      office: 401
   depart_id: 1
*************************** 6. row ***************************
          id: 6
        name: jack
         sex: female
         age: 18
   hire_date: 2011-02-11
        post: teacher
post_comment: NULL
      salary: 9000.00
      office: 401
   depart_id: 1
*************************** 7. row ***************************
          id: 7
        name: jenny
         sex: male
         age: 18
   hire_date: 1900-03-01
        post: teacher
post_comment: NULL
      salary: 30000.00
      office: 401
   depart_id: 1
*************************** 8. row ***************************
          id: 8
        name: sank
         sex: male
         age: 48
   hire_date: 2010-11-11
        post: teacher
post_comment: NULL
      salary: 10000.00
      office: 401
   depart_id: 1
*************************** 9. row ***************************
          id: 9
        name: 哈哈
         sex: female
         age: 48
   hire_date: 2015-03-11
        post: sale
post_comment: NULL
      salary: 3000.13
      office: 402
   depart_id: 2
*************************** 10. row ***************************
          id: 10
        name: 呵呵
         sex: female
         age: 48
   hire_date: 2010-11-01
        post: sale
post_comment: NULL
      salary: 2000.35
      office: 402
   depart_id: 2
*************************** 11. row ***************************
          id: 11
        name: 西西
         sex: female
         age: 38
   hire_date: 2011-03-12
        post: sale
post_comment: NULL
      salary: 1000.37
      office: 402
   depart_id: 2
*************************** 12. row ***************************
          id: 12
        name: 樂樂
         sex: female
         age: 18
   hire_date: 2016-05-13
        post: sale
post_comment: NULL
      salary: 3000.29
      office: 402
   depart_id: 2
*************************** 13. row ***************************
          id: 13
        name: 啦啦
         sex: female
         age: 18
   hire_date: 2017-01-27
        post: sale
post_comment: NULL
      salary: 4000.33
      office: 402
   depart_id: 2
*************************** 14. row ***************************
          id: 14
        name: 僧龍
         sex: male
         age: 28
   hire_date: 2016-03-11
        post: operation
post_comment: NULL
      salary: 10000.13
      office: 403
   depart_id: 3
*************************** 15. row ***************************
          id: 15
        name: 程咬金
         sex: male
         age: 18
   hire_date: 1997-03-12
        post: operation
post_comment: NULL
      salary: 20000.00
      office: 403
   depart_id: 3
*************************** 16. row ***************************
          id: 16
        name: 程咬銀
         sex: female
         age: 18
   hire_date: 2013-03-11
        post: operation
post_comment: NULL
      salary: 18000.00
      office: 403
   depart_id: 3
*************************** 17. row ***************************
          id: 17
        name: 程咬銅
         sex: male
         age: 18
   hire_date: 2015-04-11
        post: operation
post_comment: NULL
      salary: 19000.00
      office: 403
   depart_id: 3
*************************** 18. row ***************************
          id: 18
        name: 程咬鐵
         sex: female
         age: 18
   hire_date: 2014-05-12
        post: operation
post_comment: NULL
      salary: 17000.00
      office: 403
   depart_id: 3
18 rows in set (0.00 sec)

   幾個重要關鍵字的執行順序

#書寫順序
select id,name from emp where id > 1;

#執行順序
from
where
select
...

"""

雖然執行順序和書寫順序不一致,你在寫sql陳述句的時候不知道怎么寫
你就按照書寫順序的方式寫sql
    select * 先用*占位
    之后再去補全后面的sql陳述句
    最后將*替換成你想要的具體欄位
    
"""    

  where篩選條件

# 作用:是對整體資料的一個篩選操作
# 1.查詢id大于等于3小于等于6的資料
select id,name,age from emp where id >=3 and id <=6;
select id ,name,age from emp where id between 3 and 6;

#2.查詢薪資是20000或者18000或者17000的資料
select * from emp where salary in (20000,18000,17000);
select * from emp where salary = 18000 or salary=20000 or salary=17000;

#3.查詢員工姓名中包含字母o的員工姓名和  薪資
"""
模糊查詢
    like    % 匹配任意多個字符,      _匹配任意單個字符
"""
select name,salary from emp where name like "%o%"; 

#4.查詢員工姓名是由4個字符組成的姓名和薪資
select name,salary from emp where name like "____";
select name,salary from emp where char_length(name) = 4;

#5.查詢id小于小于3或者大于6的資料
select * from emp where id not between 3 and 6;

#6.查詢薪資不在20000,18000,17000范圍的資料
select * from emp where salary not in (20000,18000,17000);

#7.查詢崗位描述為空的員工姓名和崗位
select name,post from emp where post_comment is null;

  group by 分組

#分組實際應用場景非常多
    男女比例
    部門平均薪資
    部門禿頭率
    國家之間的資料統計

# 1.按照部門分組
select * from emp group by post;
分組之后,最小可操作的單位應該還是組,而不再是組內的單個資料
    上述命令在你沒有設定嚴格模式的時候,是可以正常執行的,回傳的是分組之后,
每個組的第一條資料,但是這個不符合分組的規范,分組之后不應該在考慮單個資料,而是以組為操作單位, 如果設定了嚴格模式,那么上述命令會直接報錯
""" """ 如果sql_mode中的配置有:ONLY_FULL_GROUP_BY,執行上面的陳述句匯報錯: ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column 'day47.emp.id' which is not functionally dependent on columns in GROUP
BY clause; this is incompatible with sql_mode=only_full_group_by
解決辦法: 去掉sql_mode中的ONLY_FULL_GROUP_BY即可 1.執行 set global sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,
ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
'; 2.查看模式:show variables like "%mode"; 3.重新執行select * from emp group by post;即可 """ #記得最后設定回去,因為分組之后,最小可操縱的單位應該是組,而不再是組內的某個資料 set global sql_mode ='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,
NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'; 設定嚴格模式后,分組默認只能拿到分組的依據 select post from emp group by post; 按照什么分組就能只能拿到分組,其他欄位不能直接獲取,需要借助一些方法(聚合函式)
""" 練習題: #什么時候需要分組??? 關鍵字:每個,平均,最高,最低 #1.獲得每個部門的最高薪資 select post,max(salary) from emp group by post; mysql> select post, max(salary) from emp group by post; +-----------------------+-------------+ | post | max(salary) | +-----------------------+-------------+ | operation | 20000.00 | | sale | 4000.33 | | teacher | 1000000.31 | | 張江第一帥形象 | 7300.33 | +-----------------------+-------------+ 4 rows in set (0.01 sec) select post as '部門' ,max(salary) as '最高薪資' from emp group by post; mysql> select post as '部門',max(salary) as '最高薪資' from emp group by post; +-----------------------+--------------+ | 部門 | 最高薪資 | +-----------------------+--------------+ | operation | 20000.00 | | sale | 4000.33 | | teacher | 1000000.31 | | 張江第一帥形象 | 7300.33 | +-----------------------+--------------+ 4 rows in set (0.01 sec) #as 可以給欄位起別名,也可以直接省略不寫,但是不推薦,因為省略的話語意不明確,容易錯亂 #2.獲取每個部門的最低薪資 select post as '部門',min(salary) as min_sal from emp group by post; #3.獲取每個部門的平均薪資 select post as '部門',avg(salary) as '平均薪資' from emp group by post; #4.獲取每個部門的總薪資 selece post as '部門', sum(salary) as '總薪資' from emp group by post; #5.獲取每個部門的人數 select post as '部門',count(id) as '總人數' from emp group by post; select post as '部門',count(name) as '總人數' from emp group by post; select post as '部門',count(age) as '總人數' from emp group by post; select post as '部門',count(salary) as '總人數' from emp group by post; select post as '部門',count(post_comment) as '總人數' from emp group by post; # 不能對null進行計數,其他都可以 #6.查詢分組之后的部門名稱,和每個部門下所有員工的姓名 #group_concat 不單單支持你獲取分組之后的其他欄位,還支持拼接操作 select post as '部門',group_coucat(name) as '部門成員' from emp group by post; select post as '部門',group_concat(name,'_DSB') as '員工姓名' from emp group by post; select post,group_concat(name,':',salary) from emp group by post; #concat 不分組的時候使用 select concat('NAME:',name),concat("SAL:",salary) from emp; # 補充:as語法不單單可以給欄位起別名,還可以給表取別名 select emp.id,emp.name from emp; select t1.id,t1.name from emp as t1; #7.查詢每個人的年薪 12薪 select name,salary * 12 from emp;

   分組注意事項

"""
from
where
group by
"""

#關鍵字where和group by 同時出現的時候,group by必須在where后面
where先對整個資料進行過濾,之后再分組操作
where篩選條件不能使用聚合函式

select id,name,salary from emp where max(salary) > 3000;

mysql> select id,name,age from emp where max(salary) > 3000;
ERROR 1111 (HY000): Invalid use of group function    

select max(salary) from emp;        #不分組默認就是一組

#統計各部門年齡在30歲以上的員工平均工資
select post as '部門', group_concat(name) as '員工姓名', avg(salary) from emp where age > 30 group by post;

  having 分組之后的篩選條件

"""
having 的語法是跟where是一致的,只不過having是在分組之后使用的過濾操作,即having是可以使用聚合函式的
"""

#統計各部門年齡在30歲以上的員工的平均工資并且保留平均薪資大于10000的部門

select post,avg(salary) from emp where age > 30 group by post having avg(salary) >10000;

  distinct 去重

 

"""
一定要注意,必須是完全一樣的資料才可以去重!!!!!
一定不要將逐漸忽視了,有主鍵存在的情況下是一定不可能去重的
"""

[
{'id':1,'name':'jason','age':18},
{'id':2,'name':'jason','age':18},
{'id':3,'name':'egon','age':18}
]
ORM  物件關系映射        讓不懂sql陳述句的人也能夠非常牛逼的操作資料庫
表                            類
一條條資料                    物件
欄位對應的值                  物件的屬性
你再寫類,就意味著在創建表 
用類生成物件,就意味著在創建資料
物件.屬性,就是在獲取資料欄位對應的值
目的就是減輕Python程式員的壓力,只需要會Python面向物件的知識點就可以操作mysql
"""
select distinct id,age from emp;    #帶有主鍵,無法去重
select distince age from emp;

 

  order by排序

select * from emp order by salary;
select * from emp order by salary asc;
select * from emp order by salary desc;

"""
order by 默認是升序,asc   可以忽略不寫
order by desc 降序

"""

select * from emp order by age desc,salary asc;
#先按照age降序排列,如果age相同,則再按照salary升序排列

# 統計各部門年齡在10歲以上的員工平均工資并且保留平均薪資大于1000的部門,然后對平均工資進行排序
select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary) desc;

 

  limit限制條數

select * from emp;
"""
針對資料過多的情況,我們通常是都是做分頁處理
"""

select * from emp limit 3;    #只展示3條資料
select * from emp limit 0,5;
select * from emp limit 5,5;
#第一條資料表示起始位置
#第二條資料表示取得條數

  正則

select * from emp where name regexp '^j.*(n|y)$'
#上面的正則運算式表示:以j開頭,以n或者y結尾,中間任意多個字符

多表操作理論

  前期表準備

前期表準備

# 建表
create table dep(
    id int,
    name varchar(20) 
);

create table emp(
    id int primary key auto_increment,
    name varchar(20),
    sex enum('male','female') not null default 'male',
    age int,
    dep_id int
);

#插入資料
insert into dep values
(200,'技術'),
(201,'人力資源'),
(202,'銷售'),
(203,'運營');

insert into emp(name,sex,age,dep_id) values
('jason','male',18,200),
('egon','female',48,201),
('kevin','male',18,201),
('nick','male',28,202),
('owen','male',18,203),
('jerry','female',18,204);

  表查詢

select * from dep,emp;    #結果叫笛卡爾積

mysql> select * from dep,emp;
+------+--------------+----+-------+--------+------+--------+
| id   | name         | id | name  | sex    | age  | dep_id |
+------+--------------+----+-------+--------+------+--------+
|  200 | 技術         |  1 | jason | male   |   18 |    200 |
|  201 | 人力資源     |  1 | jason | male   |   18 |    200 |
|  202 | 銷售         |  1 | jason | male   |   18 |    200 |
|  203 | 運營         |  1 | jason | male   |   18 |    200 |
|  200 | 技術         |  2 | egon  | female |   48 |    201 |
|  201 | 人力資源     |  2 | egon  | female |   48 |    201 |
|  202 | 銷售         |  2 | egon  | female |   48 |    201 |
|  203 | 運營         |  2 | egon  | female |   48 |    201 |
|  200 | 技術         |  3 | kevin | male   |   18 |    201 |
|  201 | 人力資源     |  3 | kevin | male   |   18 |    201 |
|  202 | 銷售         |  3 | kevin | male   |   18 |    201 |
|  203 | 運營         |  3 | kevin | male   |   18 |    201 |
|  200 | 技術         |  4 | nick  | male   |   28 |    202 |
|  201 | 人力資源     |  4 | nick  | male   |   28 |    202 |
|  202 | 銷售         |  4 | nick  | male   |   28 |    202 |
|  203 | 運營         |  4 | nick  | male   |   28 |    202 |
|  200 | 技術         |  5 | owen  | male   |   18 |    203 |
|  201 | 人力資源     |  5 | owen  | male   |   18 |    203 |
|  202 | 銷售         |  5 | owen  | male   |   18 |    203 |
|  203 | 運營         |  5 | owen  | male   |   18 |    203 |
|  200 | 技術         |  6 | jerry | female |   18 |    204 |
|  201 | 人力資源     |  6 | jerry | female |   18 |    204 |
|  202 | 銷售         |  6 | jerry | female |   18 |    204 |
|  203 | 運營         |  6 | jerry | female |   18 |    204 |
+------+--------------+----+-------+--------+------+--------+
24 rows in set (0.00 sec)


#上面的資料不是我們想要的,我們想要直接把對應的部門添加到每個員工后面
select * from emp,dep where emp.dep_id = dep.id;

mysql> select * from emp ,dep where emp.dep_id = dep.id;
+----+-------+--------+------+--------+------+--------------+
| id | name  | sex    | age  | dep_id | id   | name         |
+----+-------+--------+------+--------+------+--------------+
|  1 | jason | male   |   18 |    200 |  200 | 技術         |
|  2 | egon  | female |   48 |    201 |  201 | 人力資源     |
|  3 | kevin | male   |   18 |    201 |  201 | 人力資源     |
|  4 | nick  | male   |   28 |    202 |  202 | 銷售         |
|  5 | owen  | male   |   18 |    203 |  203 | 運營         |
+----+-------+--------+------+--------+------+--------------+
5 rows in set (0.00 sec)

"""
mysql也知道,你在后面查詢資料的程序中,肯定會經常用到這樣的操作
所以特定給我們提供了對應的方法
        inner join     內連接
        lefy join       左連接
        right join      右連接
        union     全連接
"""

#inner join
select * from emp inner join dep on emp.dep_id = dep.id;
#只拼接兩張表中共有的資料部分
mysql> select * from emp inner join dep on emp.dep_id = dep.id;
+----+-------+--------+------+--------+------+--------------+
| id | name  | sex    | age  | dep_id | id   | name         |
+----+-------+--------+------+--------+------+--------------+
|  1 | jason | male   |   18 |    200 |  200 | 技術         |
|  2 | egon  | female |   48 |    201 |  201 | 人力資源     |
|  3 | kevin | male   |   18 |    201 |  201 | 人力資源     |
|  4 | nick  | male   |   28 |    202 |  202 | 銷售         |
|  5 | owen  | male   |   18 |    203 |  203 | 運營         |
+----+-------+--------+------+--------+------+--------------+
5 rows in set (0.00 sec)

#left join
#left join
select * from emp left join dep on emp.dep_id = dep.id;
#左表所有的資料都顯示出來,沒有對應的資料用null代替
mysql> select * from emp left join dep on emp.dep_id = dep.id;
+----+-------+--------+------+--------+------+--------------+
| id | name  | sex    | age  | dep_id | id   | name         |
+----+-------+--------+------+--------+------+--------------+
|  1 | jason | male   |   18 |    200 |  200 | 技術         |
|  2 | egon  | female |   48 |    201 |  201 | 人力資源     |
|  3 | kevin | male   |   18 |    201 |  201 | 人力資源     |
|  4 | nick  | male   |   28 |    202 |  202 | 銷售         |
|  5 | owen  | male   |   18 |    203 |  203 | 運營         |
|  6 | jerry | female |   18 |    204 | NULL | NULL         |
+----+-------+--------+------+--------+------+--------------+
6 rows in set (0.05 sec)

#right join
select * from emp right join dep on emp.dep_id = dep.id;
#右表所有的資料都顯示出來,沒有對應的資料用null代替
mysql> select * from emp right join dep on emp.dep_id = dep.id;
+------+-------+--------+------+--------+------+--------------+
| id   | name  | sex    | age  | dep_id | id   | name         |
+------+-------+--------+------+--------+------+--------------+
|    1 | jason | male   |   18 |    200 |  200 | 技術         |
|    2 | egon  | female |   48 |    201 |  201 | 人力資源     |
|    3 | kevin | male   |   18 |    201 |  201 | 人力資源     |
|    4 | nick  | male   |   28 |    202 |  202 | 銷售         |
|    5 | owen  | male   |   18 |    203 |  203 | 運營         |
| NULL | NULL  | NULL   | NULL |   NULL |  205 | 公關部       |
+------+-------+--------+------+--------+------+--------------+
6 rows in set (0.00 sec)

#union
select * from emp left join dep on emp.dep_id = dep.id
union
select * from emp right join dep on emp.dep_id = dep.id;

mysql> select * from emp left join dep on emp.dep_id = dep.id
    -> union
    -> select * from emp right join dep on emp.dep_id = dep.id;
+------+-------+--------+------+--------+------+--------------+
| id   | name  | sex    | age  | dep_id | id   | name         |
+------+-------+--------+------+--------+------+--------------+
|    1 | jason | male   |   18 |    200 |  200 | 技術         |
|    2 | egon  | female |   48 |    201 |  201 | 人力資源     |
|    3 | kevin | male   |   18 |    201 |  201 | 人力資源     |
|    4 | nick  | male   |   28 |    202 |  202 | 銷售         |
|    5 | owen  | male   |   18 |    203 |  203 | 運營         |
|    6 | jerry | female |   18 |    204 | NULL | NULL         |
| NULL | NULL  | NULL   | NULL |   NULL |  205 | 公關部       |
+------+-------+--------+------+--------+------+--------------+
7 rows in set (0.01 sec)

  子查詢

 

"""
子查詢就是我們平時解決問題的思路
    分步驟解決問題
        第一步
       第二步

將一個查詢的結果,當做另外一個查詢陳述句的條件去使用
"""

#查詢部門是技識訓者人力資源的員工資訊

#1.先獲取符合條件的部門id
select id from dep where name='技術' or name = '人力資源部';

#2.再去員工表里篩選出對應的員工

select name from emp where emp.dep_id in (select id from dep where name='技術' or name = '人力資源部');

 

  總結

表的查詢結果可以作為其他表的查詢條件
也可以通過起別名的方式把他作為一張虛擬表跟其他表關聯
"""
多表查詢就兩種:
    先拼接表再查詢
    子查詢:一步一步來查詢
"""

# 關鍵字 exists(了解)
只回傳布林值 True False
回傳True的時候,外層查詢陳述句執行
回傳False的時候,外層查詢陳述句不再執行

select * from emp where exists (select id from dep where id > 203);

mysql> select * from emp where exists (select id from dep where id > 203);
+----+-------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+-------+--------+------+--------+
| 1 | jason | male | 18 | 200 |
| 2 | egon | female | 48 | 201 |
| 3 | kevin | male | 18 | 201 |
| 4 | nick | male | 28 | 202 |
| 5 | owen | male | 18 | 203 |
| 6 | jerry | female | 18 | 204 |
+----+-------+--------+------+--------+
6 rows in set (0.00 sec)


select * from emp where exists (select id from dep where id > 206);
mysql> select * from emp where exists (select id from dep where id > 206);
Empty set (0.00 sec)

 

 

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/297715.html

標籤:Python

上一篇:四十六、MySQL資料庫3

下一篇:leetcode常用函式刷題必備(一)

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more