MYSQL 多表操作<六>

1)承接上一节内容
mysql> show tables;
+------------------------+
| Tables_in_shoppingmall |
+------------------------+
| category               |
| orderItem              |
| orders                 |
| product                |
| user                   |
+------------------------+
5 rows in set (0.01 sec)
2)多表查询内连接

 -- 笛卡尔积: 查出来的是两张表的乘积,查出来没有实际意义
 select * from product,category;

 -- 过滤出有意义的数据
 select * from product,category where cno = cid;

 select * from product as p,category as c where p.cno = c.cid;
 select * from product  p,category c where p.cno = c.cid;

-- 内连接
  隐式内连接
  select * from product p,category c where p.cno = c.cid;

  显式内连接
  select * from product p inner join category c on p.cno = c.cid;

-- 区别:
  隐式内连接:在查询出结果的基础上去做 where 条件过滤
  显式内连接:带着条件去查询结果,只需的效率更高

  • 数据如下
mysql> select * from product as p,category as c where p.cno = c.cid;
+-----+--------------+-------+------+-----+--------------+-----------------+
| pid | pname        | price | cno  | cid | cname        | cdesc           |
+-----+--------------+-------+------+-----+--------------+-----------------+
|   1 | iphone       |  5300 |    1 |   1 | 电器         | 电器类商品      |
|   3 | 茅台         |  1800 |    2 |   2 | 副食品       | 烟酒类          |
|   9 | 五粮液       |   800 |    2 |   2 | 副食品       | 烟酒类          |
|   4 | 卫生纸       |     5 |    3 |   3 | 生活用品     | 用于生活        |
|   5 | 香蕉         |    10 |    4 |   4 | 水果         | 吃吃更健康      |
|   8 | 葡萄         |    15 |    4 |   4 | 水果         | 吃吃更健康      |
|   6 | 大白菜       |    30 |    5 |   5 | 蔬菜         | 绿色食品        |
|   7 | 三只松鼠     |   100 |    6 |   6 | 干果         | 好吃的零食      |
+-----+--------------+-------+------+-----+--------------+-----------------+
8 rows in set (0.00 sec)
3)左外、右外连接查询
--  左外连接
     左外连接,会将左表中的所有数据都查询出来,如果右表中没有对应的数据,则用null代替

准备工作:插入数据(cno = null)
insert into `product` values(null,'水杯',25,null);

select * from product p left outer join category c on p.cno = c.cid;

--  右外连接
      右外连接,会将右表中的所有数据查询出来,如果左表没有对应数据,则用null代替
准备工作:插入数据
insert into `category` values(100,'肉类','补充蛋白质');

select * from product p right outer join category c on p.cno = c.cid;

  • 如下图
MYSQL 多表操作<六>_第1张图片
lianjie.png
  • 左外连接数据如下
mysql> insert into `product` values(null,'水杯',25,null);
Query OK, 1 row affected (0.08 sec)

mysql> select * from product;
+-----+--------------+-------+------+
| pid | pname        | price | cno  |
+-----+--------------+-------+------+
|   1 | iphone       |  5300 |    1 |
|   3 | 茅台         |  1800 |    2 |
|   4 | 卫生纸       |     5 |    3 |
|   5 | 香蕉         |    10 |    4 |
|   6 | 大白菜       |    30 |    5 |
|   7 | 三只松鼠     |   100 |    6 |
|   8 | 葡萄         |    15 |    4 |
|   9 | 五粮液       |   800 |    2 |
|  10 | 水杯         |    25 | NULL |
+-----+--------------+-------+------+
9 rows in set (0.00 sec)

mysql> select * from product p left outer join category c on p.cno = c.cid;
+-----+--------------+-------+------+------+--------------+-----------------+
| pid | pname        | price | cno  | cid  | cname        | cdesc           |
+-----+--------------+-------+------+------+--------------+-----------------+
|   1 | iphone       |  5300 |    1 |    1 | 电器         | 电器类商品      |
|   3 | 茅台         |  1800 |    2 |    2 | 副食品       | 烟酒类          |
|   4 | 卫生纸       |     5 |    3 |    3 | 生活用品     | 用于生活        |
|   5 | 香蕉         |    10 |    4 |    4 | 水果         | 吃吃更健康      |
|   6 | 大白菜       |    30 |    5 |    5 | 蔬菜         | 绿色食品        |
|   7 | 三只松鼠     |   100 |    6 |    6 | 干果         | 好吃的零食      |
|   8 | 葡萄         |    15 |    4 |    4 | 水果         | 吃吃更健康      |
|   9 | 五粮液       |   800 |    2 |    2 | 副食品       | 烟酒类          |
|  10 | 水杯         |    25 | NULL | NULL | NULL         | NULL            |
+-----+--------------+-------+------+------+--------------+-----------------+
9 rows in set (0.00 sec)
  • 右外连接数据如下
mysql> select * from category;
+-----+--------------+-----------------+
| cid | cname        | cdesc           |
+-----+--------------+-----------------+
|   1 | 电器         | 电器类商品      |
|   2 | 副食品       | 烟酒类          |
|   3 | 生活用品     | 用于生活        |
|   4 | 水果         | 吃吃更健康      |
|   5 | 蔬菜         | 绿色食品        |
|   6 | 干果         | 好吃的零食      |
| 100 | 肉类         | 补充蛋白质      |
+-----+--------------+-----------------+
7 rows in set (0.00 sec)

mysql> select * from product p right outer join category c on p.cno = c.cid;
+------+--------------+-------+------+-----+--------------+-----------------+
| pid  | pname        | price | cno  | cid | cname        | cdesc           |
+------+--------------+-------+------+-----+--------------+-----------------+
|    1 | iphone       |  5300 |    1 |   1 | 电器         | 电器类商品      |
|    3 | 茅台         |  1800 |    2 |   2 | 副食品       | 烟酒类          |
|    9 | 五粮液       |   800 |    2 |   2 | 副食品       | 烟酒类          |
|    4 | 卫生纸       |     5 |    3 |   3 | 生活用品     | 用于生活        |
|    5 | 香蕉         |    10 |    4 |   4 | 水果         | 吃吃更健康      |
|    8 | 葡萄         |    15 |    4 |   4 | 水果         | 吃吃更健康      |
|    6 | 大白菜       |    30 |    5 |   5 | 蔬菜         | 绿色食品        |
|    7 | 三只松鼠     |   100 |    6 |   6 | 干果         | 好吃的零食      |
| NULL | NULL         |  NULL | NULL | 100 | 肉类         | 补充蛋白质      |
+------+--------------+-------+------+-----+--------------+-----------------+
9 rows in set (0.00 sec)

3)分页查询

分页查询 用到关键字 limit,第一个参数是索引,第二个参数是个数


select * from product limit 0,3;

select * from product limit 3,3;

  • 分页查询数据如下
mysql> select * from product limit 0,3;
+-----+-----------+-------+------+
| pid | pname     | price | cno  |
+-----+-----------+-------+------+
|   1 | iphone    |  5300 |    1 |
|   3 | 茅台      |  1800 |    2 |
|   4 | 卫生纸    |     5 |    3 |
+-----+-----------+-------+------+
3 rows in set (0.00 sec)

mysql> select * from product limit 3,3;
+-----+--------------+-------+------+
| pid | pname        | price | cno  |
+-----+--------------+-------+------+
|   5 | 香蕉         |    10 |    4 |
|   6 | 大白菜       |    30 |    5 |
|   7 | 三只松鼠     |   100 |    6 |
+-----+--------------+-------+------+
3 rows in set (0.00 sec)

3)子查询

00

-- 查询分类名称为“水果”的所有商品
1.查询分类名为“水果”的分类id
select cid from category where cname = '水果';

2.查询出id为4的结果
select * from product where cno = 4;

3.合并
select * from product where cno = (select cid from category where cname = '水果');


-- 查询出(商品名称、商品分类名称)信息

  -- 左查询【查询出所有的信息了,不合适】
    select * from product p left outer join category c on p.cno = c.cid;

  -- 子查询
    select pname,cno from product;  //不合适

    select pname,(select cname from category where product.cno = category.cid) from product;    //不是很完美

    select pname,(select cname from category where product.cno = category.cid) as 商品分类信息 from product;  //尽管满足条件,但是不美观

//最优答案
select pname,(select cname from category as c where p.cno = c.cid) as 商品分类信息 from product as p; 

//省略as
select pname,(select cname from category c where p.cno = c.cid) 商品分类信息 from product p; 
  • 子查询数据如下
`查询分类名称为“水果”的所有商品`
mysql> select cid from category where cname = '水果';
+-----+
| cid |
+-----+
|   4 |
+-----+
1 row in set (0.00 sec)

mysql> select * from product where con = 4;
ERROR 1054 (42S22): Unknown column 'con' in 'where clause'
mysql> select * from product where cno = 4;
+-----+--------+-------+------+
| pid | pname  | price | cno  |
+-----+--------+-------+------+
|   5 | 香蕉   |    10 |    4 |
|   8 | 葡萄   |    15 |    4 |
+-----+--------+-------+------+
2 rows in set (0.00 sec)

mysql> select * from product where cno = (select cid from category where cname = '水果');
+-----+--------+-------+------+
| pid | pname  | price | cno  |
+-----+--------+-------+------+
|   5 | 香蕉   |    10 |    4 |
|   8 | 葡萄   |    15 |    4 |
+-----+--------+-------+------+
2 rows in set (0.00 sec)

`查询出(商品名称、商品分类名称)信息`
mysql> select pname,(select cname from category c where p.cno = c.cid) 商品分类信息 from product  p; 
+--------------+--------------------+
| pname        | 商品分类信息       |
+--------------+--------------------+
| iphone       | 电器               |
| 茅台         | 副食品             |
| 卫生纸       | 生活用品           |
| 香蕉         | 水果               |
| 大白菜       | 蔬菜               |
| 三只松鼠     | 干果               |
| 葡萄         | 水果               |
| 五粮液       | 副食品             |
| 水杯         | NULL               |
+--------------+--------------------+
9 rows in set (0.00 sec)

你可能感兴趣的:(MYSQL 多表操作<六>)