1. 程式人生 > 實用技巧 >資料庫之mysql多表查詢(連表)以及pymysql等相關內容-45

資料庫之mysql多表查詢(連表)以及pymysql等相關內容-45

1.多表查詢

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

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

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

insert into employee(name,sex,age,dep_id) values
('egon','male',18,200),
('alex','female',48,201),
('wupeiqi','male',38,201),
('yuanhao','female',28,202),
('liwenzhou','male',18,200),
('jingliyang','female',18,204)
;


#查看錶結構和資料
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

mysql> desc employee;
+--------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | | NULL | |
+--------+-----------------------+------+-----+---------+----------------+

mysql> select * from department;
+------+--------------+
| id | name |
+------+--------------+
| 200 | 技術 |
| 201 | 人力資源 |
| 202 | 銷售 |
| 203 | 運營 |
+------+--------------+

mysql> select * from employee;
+----+------------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+------------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | female | 18 | 204 |
+----+------------+--------+------+--------+

表department與employee
多表聯合查詢
# 方案1:連結串列
把多張物理表合併成一張虛擬表,再進行後續查詢

#======>內連結:保留兩張表有對應關係的記錄
select * from emp,dep where emp.dep_id=dep.id;
select dep.name,emp.name from emp inner join dep on emp.dep_id=dep.id
where dep.name = "技術";

#======>左連結:在內連結的基礎上保留左表的記錄
select * from emp left join dep on emp.dep_id=dep.id;

#======>右連結:在內連結的基礎上保留右表的記錄
select * from emp right join dep on emp.dep_id=dep.id;

#======>全外連結:在內連結的基礎上保留左右表的記錄
full join

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;


示例1:查詢所有部門名及對應的員工個數
select dep.name,count(emp.id) from emp right join dep on emp.dep_id = dep.id
group by dep.name
;

select dep.name,count(emp.id) from emp right join dep on emp.dep_id = dep.id
group by dep.name
having count(emp.id) < 2
;

#示例2:即找出年齡大於25歲的員工以及員工所在的部門
select emp.name,dep.name from emp inner join dep on emp.dep_id = dep.id where age > 25;


#示例3:以內連線的方式查詢employee和department表,並且以age欄位的升序方式顯示


# 把多張錶鏈接到一起:
select * from
(select emp.*,dep.name as dep_name from emp inner join dep on emp.dep_id = dep.id) as t1
inner join
dep
on t1.dep_id = dep.id
;

select * from emp
inner join dep
on emp.dep_id = dep.id

inner join dep as t1
on t1.id = dep.id;


# 查詢部門內最新入職的員工
select * from employee
inner join
(select depart_id,max(hire_date) as maxd from employee group by depart_id) as t1
on employee.depart_id = t1.depart_id
where employee.hire_date = t1.maxd
;


# 方案2:子查詢
從一張表中查詢出結果,用該結果作為查
詢下一張表的過濾條件
select * from employee
where hire_date = (select max(hire_date) from employee);


#查詢平均年齡在25歲以上的部門名
select * from dep where id in
(select dep_id from emp group by dep_id having avg(age) > 25);

#檢視技術部員工姓名
select * from emp where dep_id in
(select id from dep where name="技術");

#檢視不足1人的部門名(子查詢得到的是有人的部門id)
select * from dep where id not in (select distinct dep_id from emp);

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

2.pymysql

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123', database='db4', charset='utf8mb4')
# 遊標
cursor = conn.cursor() # 執行•完畢返回的結果集預設以元組顯示

# cursor.execute("insert into user(name,pwd) values('egon','123'),('tom','456'),('jack','111');")

# sql="insert into user(name,pwd) values('%s','%s');" %('lili','123')
# cursor.execute(sql)

# %s不要加引號
# cursor.execute("insert into user(name,pwd) values(%s,%s);",('kkk','123'))


username = input("username>>>: ").strip()
password = input("password>>>: ").strip()
# sql = "select * from user where name='%s' and pwd='%s'" %(username,password)
# select * from user where name='egon' -- hello' and pwd='%s'
# select * from user where name='xxx' or 1=1 -- hello' and pwd='%s';
# rows=cursor.execute(sql)

rows = cursor.execute("select * from user where name=%s and pwd=%s", (username, password))

if rows:
print('ok')
else:
print('no')

conn.commit()
cursor.close()
conn.close()