目錄python
將一條sql語句執行的結果做爲另外一個sql語句的條件
# 如下是基於對錶的增刪改查操做的 1.增數據時: insert into 表名 select 子查詢 註解: 對一個表插入數據時,插入的數據能夠是另一個sql語句查詢結果,可是首先要建立這個表 2.刪除數據時: delete from 表名 條件是 select子查詢(表不能與delete表相同) 註解: 刪除一個表的某些數據時,他的條件是另外一個sql查詢語句結果 3.查: select 字段 from 表 條件是select子查詢 註解: 查詢一個表的數據的條件是一個sql語句的執行結果 4.改: update 表 set 字段=值 條件是select子查詢(表不能與update表相同) 註解: 更新一個表的數據,他的條件是另外一個sql語句的執行結果
# 數據來源:在單表emp下 # 此處顯示emp的數據 mysql> select * from emp; +----+------+------+------+--------+--------+-----------+-----------+ | id | name | sex | age | salary | area | addr | dep | +----+------+------+------+--------+--------+-----------+-----------+ | 1 | aaa | 男 | 42 | 10.5 | 上海 | 浦東 | 教職部 | | 2 | bbb | 男 | 38 | 9.4 | 山東 | 濟南 | 教學部 | | 3 | ccc | 女 | 30 | 3 | 江蘇 | 張家港 | 教學部 | | 4 | ddd | 女 | 28 | 2.4 | 廣州 | 廣東 | 教學部 | | 5 | eee | 男 | 28 | 2.4 | 江蘇 | 蘇州 | 教學部 | | 6 | fff | 男 | 18 | 8.8 | 中國 | 黃浦 | 諮詢部 | | 7 | ggg | 男 | 18 | 8.8 | 安徽 | 宣城 | 教學部 | | 8 | hhh | 男 | 28 | 9.8 | 安徽 | 巢湖 | 教學部 | | 9 | iii | 女 | 36 | 1.2 | 安徽 | 蕪湖 | 諮詢部 | | 10 | jjj | 男 | 36 | 5.8 | 山東 | 濟南 | 教學部 | | 11 | kkk | 女 | 28 | 1.2 | 山東 | 青島 | 教職部 | | 12 | lll | 男 | 30 | 9 | 上海 | 浦東 | 諮詢部 | | 13 | mmm | 男 | 30 | 6 | 上海 | 浦東 | 諮詢部 | | 14 | nnn | 男 | 30 | 6 | 上海 | 浦西 | 教學部 | | 15 | ooo | 女 | 67 | 2.501 | 上海 | 陸家嘴 | 教學部 | +----+------+------+------+--------+--------+-----------+-----------+
# 題目:每一個部門最高薪資的那我的全部信息 1.先建立一個子查詢sql(對emp表的部門進行分組,查詢每一個部門的最高薪資) select dep,max(salary) from emp group by dep; mysql> select dep,max(salary) from emp group by dep; +-----------+-------------+ | dep | max(salary) | +-----------+-------------+ | 諮詢部 | 9 | | 教學部 | 9.8 | | 教職部 | 10.5 | +-----------+-------------+ 2.查操做:經過子查詢去進行對emp數據的查找(將子查詢做爲查詢每一個部門工資最高的員工的全部信息) select * from emp where (dep,salary) in (select dep,max(salary) from emp group by dep); mysql> select * from emp where (dep,salary) in (select dep,max(salary) from emp group by dep); +----+------+------+------+--------+--------+--------+-----------+ | id | name | sex | age | salary | area | addr | dep | +----+------+------+------+--------+--------+--------+-----------+ | 1 | aaa | 男 | 42 | 10.5 | 上海 | 浦東 | 教職部 | | 8 | hhh | 男 | 28 | 9.8 | 安徽 | 巢湖 | 教學部 | | 12 | lll | 男 | 30 | 9 | 上海 | 浦東 | 諮詢部 | +----+------+------+------+--------+--------+--------+-----------+ 3.增長數據操做:經過子查詢去對一張表進行增長數據操做 1.首先是先建立一張空表 create table t1(dep_name varchar(64), max_salary decimal(5,2)); 2.將子查詢的結果做爲數據添加到t1表裏(將子查詢的sql語句直接做爲t1表的插入數據) insert into t1 select dep,max(salary) from emp group by dep; mysql> select * from t1; +-----------+------------+ | dep_name | max_salary | +-----------+------------+ | 諮詢部 | 9.00 | | 教學部 | 9.80 | | 教職部 | 10.50 | +-----------+------------+ 3.完成上述題目的需求(查出每一個部門中薪資最高的員工的姓名,部門,工資) select name,dep_name,salary from emp join t1 on emp.dep=t1.dep_name and emp.salary=t1.max_salary; mysql> select name,dep_name,salary from emp join t1 on emp.dep=t1.dep_name and emp.salary=t1.max_salary; +------+-----------+--------+ | name | dep_name | salary | +------+-----------+--------+ | aaa | 教職部 | 10.5 | | lll | 諮詢部 | 9 | +------+-----------+--------+ 4.更新操做:給(update更新的表不能 與 子查詢select的表同表) 1.給每一個部門最大薪資+1 update t1 set max_salary=max_salary+1; mysql> select * from t1; +-----------+------------+ | dep_name | max_salary | +-----------+------------+ | 諮詢部 | 10.00 | | 教學部 | 10.80 | | 教職部 | 11.50 | +-----------+------------+ 2.給t1額外增長一個新部門 insert into t1 values ('打雜部', 100); mysql> select * from t1; ); +-----------+------------+ | dep_name | max_salary | +-----------+------------+ | 諮詢部 | 10.00 | | 教學部 | 10.80 | | 教職部 | 11.50 | | 打雜部 | 100.00 | +-----------+------------+ 3.修改t1表中的最大工資+1,條件是他的部門必須存在於emp表中的dep_anme中(修改的條件是一個子查詢) update t1 set max_salary=max_salary+1 where dep_name in (select distinct dep from emp); mysql> select * from t1; +-----------+------------+ | dep_name | max_salary | +-----------+------------+ | 諮詢部 | 11.00 | | 教學部 | 11.80 | | 教職部 | 12.50 | | 打雜部 | 100.00 | +-----------+------------+ 4.錯誤:update更新的表 與 子查詢select的表 相同 update t1 set max_salary=max_salary+1 where dep_name in (select distinct dep_name from t1); 5.刪除操做:刪除t1表中部門在emp中也有的部門 delete from t1 where dep_name in (select distinct dep from emp); # 錯誤: delete刪除的表 與 子查詢select的表 相同 delete from t1 where dep_name in (select distinct dep_name from t1);