练习1.三表查询
create table threeTA(
id int,
name char(5)
);
insert into threeTA
values
(1,"面包"),
(2,"跑步机"),
(3,"键盘");
create table threeTB(
name char(5),
price int
);
insert into threeTB
values
("面包",5),
("跑步机",3000),
("键盘",100);
create table threeTC(
price int,
sort char(5)
);
insert into threeTC
values
(5,"低端"),
(3000,"高端"),
(100,"中端");
select * from threeTA;
+------+--------+
| id | name |
+------+--------+
| 1 | 面包 |
| 2 | 跑步机 |
| 3 | 键盘 |
+------+--------+
select * from threeTB;
+--------+-------+
| name | price |
+--------+-------+
| 面包 | 5 |
| 跑步机 | 3000 |
| 键盘 | 100 |
+--------+-------+
select * from threeTC;
+-------+------+
| price | sort |
+-------+------+
| 5 | 低端 |
| 3000 | 高端 |
| 100 | 中端 |
+-------+------+
建表完成后,三表连查:
select id,threeTA.name,threeTB.price,sort from
threeTA left join threeTB
on threeTA.name=threeTB.name
left join threeTC
on threeTB.price=threeTC.price;
+------+--------+-------+------+
| id | name | price | sort |
+------+--------+-------+------+
| 1 | 面包 | 5 | 低端 |
| 2 | 跑步机 | 3000 | 高端 |
| 3 | 键盘 | 100 | 中端 |
+------+--------+-------+------+