当前位置:Gxlcms > mysql > MySQL基础五:子查询与链接_MySQL

MySQL基础五:子查询与链接_MySQL

时间:2021-07-01 10:21:17 帮助过:40人阅读

本篇文章参考:http://www.imooc.com/learn/122
首先下载好数据,网址上面有;

#首先录入数据:
CREATE TABLE IF NOT EXISTS tdb_goods(
    goods_id    SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    goods_name  VARCHAR(150) NOT NULL,
    goods_cate  VARCHAR(40)  NOT NULL,
    brand_name  VARCHAR(40)  NOT NULL,
    goods_price DECIMAL(15,3) UNSIGNED NOT NULL DEFAULT 0,
    is_show     BOOLEAN NOT NULL DEFAULT 1,
    is_saleoff  BOOLEAN NOT NULL DEFAULT 0
  );

INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
VALUES('R510VC 15.6英寸笔记本','笔记本','华硕','3399',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('Y400N 14.0英寸笔记本电脑','笔记本','联想','4899',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('G150TH 15.6英寸游戏本','游戏本','雷神','8499',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('X550CC 15.6英寸笔记本','笔记本','华硕','2799',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('X240(20ALA0EYCD) 12.5英寸超极本','超级本','联想','4999',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('U330P 13.3英寸超极本','超级本','联想','4299',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('SVP13226SCB 13.3英寸触控超极本','超级本','索尼','7999',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('iPad mini MD531CH/A 7.9英寸平板电脑','平板电脑','苹果','1998',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)','平板电脑','苹果','3388',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES(' iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 
 (16G WiFi版)','平板电脑','苹果','2788',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('IdeaCentre C340 20英寸一体电脑 ','台式机','联想','3499',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('Vostro 3800-R1206 台式电脑','台式机','戴尔','2899',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('iMac ME086CH/A 21.5英寸一体电脑','台式机','苹果','9188',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )','
 台式机','宏碁','3699',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('Z220SFF F4F06PA工作站','服务器/工作站','惠普','4288',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('PowerEdge T110 II服务器','服务器/工作站','戴尔','5388',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('Mac Pro MD878CH/A 专业级台式电脑','服务器/工作站','苹果','28888',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('X3250 M4机架式服务器 2583i14','服务器/工作站','IBM','6888',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('玄龙精英版 笔记本散热器','笔记本配件','九州风神','',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT);
#查看数据表;
mysql>
mysql> SHOW TABLES;
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
| provinces       |
| tdb_goods       |
| test            |
| users           |
+-----------------+
7 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM tdb_goods;
+-------------+------------------------+------+-----+---------+----------------+
| Field       | Type                   | Null | Key | Default | Extra          |
+-------------+------------------------+------+-----+---------+----------------+
| goods_id    | smallint(5) unsigned   | NO   | PRI | NULL    | auto_increment |
| goods_name  | varchar(150)           | NO   |     | NULL    |                |
| goods_cate  | varchar(40)            | NO   |     | NULL    |                |
| brand_name  | varchar(40)            | NO   |     | NULL    |                |
| goods_price | decimal(15,3) unsigned | NO   |     | 0.000   |                |
| is_show     | tinyint(1)             | NO   |     | 1       |                |
| is_saleoff  | tinyint(1)             | NO   |     | 0       |                |
+-------------+------------------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)

mysql> SELECT * FROM tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸笔记本
 goods_cate: 笔记本
 brand_name: 华硕
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸笔记本电脑
 goods_cate: 笔记本
 brand_name: 联想
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸游戏本
 goods_cate: 游戏本
 brand_name: 雷神
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸笔记本
 goods_cate: 笔记本
 brand_name: 华硕
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
*************************** 5. row ***************************
   goods_id: 5
 goods_name: X240(20ALA0EYCD) 12.5英寸超极本
 goods_cate: 超级本
 brand_name: 联想
goods_price: 4999.000
    is_show: 1
 is_saleoff: 0
*************************** 6. row ***************************
   goods_id: 6
 goods_name: U330P 13.3英寸超极本
 goods_cate: 超级本
 brand_name: 联想
goods_price: 4299.000
    is_show: 1
 is_saleoff: 0
*************************** 7. row ***************************
   goods_id: 7
 goods_name: SVP13226SCB 13.3英寸触控超极本
 goods_cate: 超级本
 brand_name: 索尼
goods_price: 7999.000
    is_show: 1
 is_saleoff: 0
*************************** 8. row ***************************
   goods_id: 8
 goods_name: iPad mini MD531CH/A 7.9英寸平板电脑
 goods_cate: 平板电脑
 brand_name: 苹果
goods_price: 1998.000
    is_show: 1
 is_saleoff: 0
*************************** 9. row ***************************
   goods_id: 9
 goods_name: iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)
 goods_cate: 平板电脑
 brand_name: 苹果
goods_price: 3388.000
    is_show: 1
 is_saleoff: 0
*************************** 10. row ***************************
   goods_id: 10
 goods_name:  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)
 goods_cate: 平板电脑
 brand_name: 苹果
goods_price: 2788.000
    is_show: 1
 is_saleoff: 0
*************************** 11. row ***************************
   goods_id: 11
 goods_name: IdeaCentre C340 20英寸一体电脑
 goods_cate: 台式机
 brand_name: 联想
goods_price: 3499.000
    is_show: 1
 is_saleoff: 0
*************************** 12. row ***************************
   goods_id: 12
 goods_name: Vostro 3800-R1206 台式电脑
 goods_cate: 台式机
 brand_name: 戴尔
goods_price: 2899.000
    is_show: 1
 is_saleoff: 0
*************************** 13. row ***************************
   goods_id: 13
 goods_name: iMac ME086CH/A 21.5英寸一体电脑
 goods_cate: 台式机
 brand_name: 苹果
goods_price: 9188.000
    is_show: 1
 is_saleoff: 0
*************************** 14. row ***************************
   goods_id: 14
 goods_name: AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )
 goods_cate: 台式机
 brand_name: 宏碁
goods_price: 3699.000
    is_show: 1
 is_saleoff: 0
*************************** 15. row ***************************
   goods_id: 15
 goods_name: Z220SFF F4F06PA工作站
 goods_cate: 服务器/工作站
 brand_name: 惠普
goods_price: 4288.000
    is_show: 1
 is_saleoff: 0
*************************** 16. row ***************************
   goods_id: 16
 goods_name: PowerEdge T110 II服务器
 goods_cate: 服务器/工作站
 brand_name: 戴尔
goods_price: 5388.000
    is_show: 1
 is_saleoff: 0
*************************** 17. row ***************************
   goods_id: 17
 goods_name: Mac Pro MD878CH/A 专业级台式电脑
 goods_cate: 服务器/工作站
 brand_name: 苹果
goods_price: 28888.000
    is_show: 1
 is_saleoff: 0
*************************** 18. row ***************************
   goods_id: 18
 goods_name:  HMZ-T3W 头戴显示设备
 goods_cate: 笔记本配件
 brand_name: 索尼
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 19. row ***************************
   goods_id: 19
 goods_name: 商务双肩背包
 goods_cate: 笔记本配件
 brand_name: 索尼
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
*************************** 20. row ***************************
   goods_id: 20
 goods_name: X3250 M4机架式服务器 2583i14
 goods_cate: 服务器/工作站
 brand_name: IBM
goods_price: 6888.000
    is_show: 1
 is_saleoff: 0
*************************** 21. row ***************************
   goods_id: 21
 goods_name:  HMZ-T3W 头戴显示设备
 goods_cate: 笔记本配件
 brand_name: 索尼
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 22. row ***************************
   goods_id: 22
 goods_name: 商务双肩背包
 goods_cate: 笔记本配件
 brand_name: 索尼
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
22 rows in set (0.01 sec)

ERROR:
No query specified

子查询正文

mysql> #子查询
mysql> #子查询是嵌套在查询内部的查询;
mysql>
mysql> #使用比较运算符的子查询;
mysql> #求价格平均值;
mysql> SELECT AVG(goods_price) FROM tdb_goods;

mysql> SELECT AVG(goods_price) FROM tdb_goods;
+------------------+
| AVG(goods_price) |
+------------------+
|     5636.3636364 |
+------------------+
1 row in set (0.03 sec)

mysql> #四舍五入
mysql> #保留到小数点后两位;  mysql> SELECT ROUND(AVG(goods_price),2) FROM tdb_goods;
+---------------------------+
| ROUND(AVG(goods_price),2) |
+---------------------------+
|                   5636.36 |
+---------------------------+
1 row in set (0.02 sec)

mysql> SELECT goods_id,goods_price FROM tdb_goods WHERE goods_price>=5636.36;
+----------+-------------+
| goods_id | goods_price |
+----------+-------------+
|        3 |    8499.000 |
|        7 |    7999.000 |
|       13 |    9188.000 |
|       17 |   28888.000 |
|       18 |    6999.000 |
|       20 |    6888.000 |
|       21 |    6999.000 |
+----------+-------------+
7 rows in set (0.00 sec)

mysql> #通过子查询实现上式;
mysql> SELECT goods_id,goods_price FROM tdb_goods WHERE goods_price>=(SELECT ROUND(AVG(goods_price),2)
 FROM tdb_goods);
+----------+-------------+
| goods_id | goods_price |
+----------+-------------+
|        3 |    8499.000 |
|        7 |    7999.000 |
|       13 |    9188.000 |
|       17 |   28888.000 |
|       18 |    6999.000 |
|       20 |    6888.000 |
|       21 |    6999.000 |
+----------+-------------+
7 rows in set (0.03 sec)

mysql> SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本';
+-------------+
| goods_price |
+-------------+
|    4999.000 |
|    4299.000 |
|    7999.000 |
+-------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM tdb_goods WHERE goods_cate='超级本'\G;
*************************** 1. row ***************************
   goods_id: 5
 goods_name: X240(20ALA0EYCD) 12.5英寸超极本
 goods_cate: 超级本
 brand_name: 联想
goods_price: 4999.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 6
 goods_name: U330P 13.3英寸超极本
 goods_cate: 超级本
 brand_name: 联想
goods_price: 4299.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 7
 goods_name: SVP13226SCB 13.3英寸触控超极本
 goods_cate: 超级本
 brand_name: 索尼
goods_price: 7999.000
    is_show: 1
 is_saleoff: 0
3 rows in set (0.00 sec)

ERROR:
No query specified

mysql> SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price>
(SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本');
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> #鉴于上述错误,子查询用三个关键字修饰
mysql> SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price>ANY
(SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本');
+----------+----------------------------------+-------------+
| goods_id | goods_name                       | goods_price |
+----------+----------------------------------+-------------+
|        2 | Y400N 14.0英寸笔记本电脑         |    4899.000 |
|        3 | G150TH 15.6英寸游戏本            |    8499.000 |
|        5 | X240(20ALA0EYCD) 12.5英寸超极本  |    4999.000 |
|        7 | SVP13226SCB 13.3英寸触控超极本   |    7999.000 |
|       13 | iMac ME086CH/A 21.5英寸一体电脑  |    9188.000 |
|       16 | PowerEdge T110 II服务器          |    5388.000 |
|       17 | Mac Pro MD878CH/A 专业级台式电脑 |   28888.000 |
|       18 |  HMZ-T3W 头戴显示设备            |    6999.000 |
|       20 | X3250 M4机架式服务器 2583i14     |    6888.000 |
|       21 |  HMZ-T3W 头戴显示设备            |    6999.000 |
+----------+----------------------------------+-------------+
10 rows in set (0.00 sec)

mysql> SELECT goods_id,goods_name,goods_price FROM tdb_goods WHERE goods_price>ALL
(SELECT goods_price FROM tdb_goods WHERE goodscate='超级本');
+----------+----------------------------------+-------------+
| goods_id | goods_name                       | goods_price |
+----------+----------------------------------+-------------+
|        3 | G150TH 15.6英寸游戏本            |    8499.000 |
|       13 | iMac ME086CH/A 21.5英寸一体电脑  |    9188.000 |
|       17 | Mac Pro MD878CH/A 专业级台式 脑 |   28888.000 |
+----------+----------------------------------+-------------+
3 rows in set (0.01 sec)

mysql>  #使用 in 和not in 引发的子查询;
mysql> #EXIST 和not EXIST 引发的子查询

mysql>  #创建商品标签数据表:
mysql> CREATE TABLE IF NOT EXISTS tdb_goods_cate(
    -> cate_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    -> cate_name VARCHAR(40) NOT NULL
    -> );
Query OK, 0 rows affected (0.18 sec)

mysql> #得到商品分类;
mysql> SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
+---------------+
| goods_cate    |
+---------------+
| 台式机        |
| 平板电脑      |
| 服务器/工作站 |
| 游戏本        |
| 笔记本        |
| 笔记本配件    |
| 超级本        |
+---------------+
7 rows in set (0.00 sec)

mysql> #将查询的结果写入的数据表中、
mysql> SELECT * FROM tdb_goods_cate;
Empty set (0.00 sec)

mysql> DESC tdb_goods_cate;
+-----------+----------------------+------+-----+---------+----------------+
| Field     | Type                 | Null | Key | Default | Extra          |
+-----------+----------------------+------+-----+---------+----------------+
| cate_id   | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| cate_name | varchar(40)          | NO   |     | NULL    |                |
+-----------+----------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

mysql> INSERT tdb_goods_cate(cate_name) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
Query OK, 7 rows affected (0.04 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM tdb_goods_cate;
+---------+---------------+
| cate_id | cate_name     |
+---------+---------------+
|       1 | 台式机        |
|       2 | 平板电脑      |
|       3 | 服务器/工作站 |
|       4 | 游戏本        |
|       5 | 笔记本        |
|       6 | 笔记本配件    |
|       7 | 超级本        |
+---------+---------------+
7 rows in set (0.00 sec)

mysql> #参照分类表更新商品表、
mysql> UPDATE tdb_goods INNER JOIN tdb_goods_cate ON goods_cate=cate_name SET goods_cate=cate_id;
Query OK, 22 rows affected (0.21 sec)
Rows matched: 22  Changed: 22  Warnings: 0

mysql> SELECT * FROM tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸笔记本
 goods_cate: 5
 brand_name: 华硕
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸笔记本电脑
 goods_cate: 5
 brand_name: 联想
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸游戏本
 goods_cate: 4
 brand_name: 雷神
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸笔记本
 goods_cate: 5
 brand_name: 华硕
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
*************************** 5. row ***************************
   goods_id: 5
 goods_name: X240(20ALA0EYCD) 12.5英寸超极本
 goods_cate: 7
 brand_name: 联想
goods_price: 4999.000
    is_show: 1
 is_saleoff: 0
*************************** 6. row ***************************
   goods_id: 6
 goods_name: U330P 13.3英寸超极本
 goods_cate: 7
 brand_name: 联想
goods_price: 4299.000
    is_show: 1
 is_saleoff: 0
*************************** 7. row ***************************
   goods_id: 7
 goods_name: SVP13226SCB 13.3英寸触控超极本
 goods_cate: 7
 brand_name: 索尼
goods_price: 7999.000
    is_show: 1
 is_saleoff: 0
*************************** 8. row ***************************
   goods_id: 8
 goods_name: iPad mini MD531CH/A 7.9英寸平板电脑
 goods_cate: 2
 brand_name: 苹果
goods_price: 1998.000
    is_show: 1
 is_saleoff: 0
*************************** 9. row ***************************
   goods_id: 9
 goods_name: iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)
 goods_cate: 2
 brand_name: 苹果
goods_price: 3388.000
    is_show: 1
 is_saleoff: 0
*************************** 10. row ***************************
   goods_id: 10
 goods_name:  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)
 goods_cate: 2
 brand_name: 苹果
goods_price: 2788.000
    is_show: 1
 is_saleoff: 0
*************************** 11. row ***************************
   goods_id: 11
 goods_name: IdeaCentre C340 20英寸一体电脑
 goods_cate: 1
 brand_name: 联想
goods_price: 3499.000
    is_show: 1
 is_saleoff: 0
*************************** 12. row ***************************
   goods_id: 12
 goods_name: Vostro 3800-R1206 台式电脑
 goods_cate: 1
 brand_name: 戴尔
goods_price: 2899.000
    is_show: 1
 is_saleoff: 0
*************************** 13. row ***************************
   goods_id: 13
 goods_name: iMac ME086CH/A 21.5英寸一体电脑
 goods_cate: 1
 brand_name: 苹果
goods_price: 9188.000
    is_show: 1
 is_saleoff: 0
*************************** 14. row ***************************
   goods_id: 14
 goods_name: AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )
 goods_cate: 1
 brand_name: 宏碁
goods_price: 3699.000
    is_show: 1
 is_saleoff: 0
*************************** 15. row ***************************
   goods_id: 15
 goods_name: Z220SFF F4F06PA工作站
 goods_cate: 3
 brand_name: 惠普
goods_price: 4288.000
    is_show: 1
 is_saleoff: 0
*************************** 16. row ***************************
   goods_id: 16
 goods_name: PowerEdge T110 II服务器
 goods_cate: 3
 brand_name: 戴尔
goods_price: 5388.000
    is_show: 1
 is_saleoff: 0
*************************** 17. row ***************************
   goods_id: 17
 goods_name: Mac Pro MD878CH/A 专业级台式电脑
 goods_cate: 3
 brand_name: 苹果
goods_price: 28888.000
    is_show: 1
 is_saleoff: 0
*************************** 18. row ***************************
   goods_id: 18
 goods_name:  HMZ-T3W 头戴显示设备
 goods_cate: 6
 brand_name: 索尼
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 19. row ***************************
   goods_id: 19
 goods_name: 商务双肩背包
 goods_cate: 6
 brand_name: 索尼
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
*************************** 20. row ***************************
   goods_id: 20
 goods_name: X3250 M4机架式服务器 2583i14
 goods_cate: 3
 brand_name: IBM
goods_price: 6888.000
    is_show: 1
 is_saleoff: 0
*************************** 21. row ***************************
   goods_id: 21
 goods_name:  HMZ-T3W 头戴显示设备
 goods_cate: 6
 brand_name: 索尼
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 22. row ***************************
   goods_id: 22
 goods_name: 商务双肩背包
 goods_cate: 6
 brand_name: 索尼
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
22 rows in set (0.00 sec)

ERROR:
No query specified

mysql> SELECT * FROM tdb_goods_brands;
+----------+------------+
| brand_id | brand_name |
+----------+------------+
|        1 | IBM        |
|        2 | 华硕       |
|        3 | 宏碁       |
|        4 | 惠普       |
|        5 | 戴尔       |
|        6 | 索尼       |
|        7 | 联想       |
|        8 | 苹果       |
|        9 | 雷神       |
+----------+------------+
9 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM tdb_goods;
+-------------+------------------------+------+-----+---------+----------------+
| Field       | Type                   | Null | Key | Default | Extra          |
+-------------+------------------------+------+-----+---------+----------------+
| goods_id    | smallint(5) unsigned   | NO   | PRI | NULL    | auto_increment |
| goods_name  | varchar(150)           | NO   |     | NULL    |                |
| goods_cate  | varchar(40)            | NO   |     | NULL    |                |
| brand_name  | varchar(40)            | NO   |     | NULL    |                |
| goods_price | decimal(15,3) unsigned | NO   |     | 0.000   |                |
| is_show     | tinyint(1)             | NO   |     | 1       |                |
| is_saleoff  | tinyint(1)             | NO   |     | 0       |                |
+-------------+------------------------+------+-----+---------+----------------+
7 rows in set (0.03 sec)

mysql> SHOW COLUMNS FROM tdb_goods_brands;
+------------+----------------------+------+-----+---------+----------------+
| Field      | Type                 | Null | Key | Default | Extra          |
+------------+----------------------+------+-----+---------+----------------+
| brand_id   | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| brand_name | varchar(40)          | NO   |     | NULL    |                |
+------------+----------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> UPDATE tdb_goods AS g INNER JOIN tdb_goods_brands AS b ON g.brand_name=b.brand_name
    -> SET g.brand_name=b.brand_id;
Query OK, 22 rows affected (0.06 sec)
Rows matched: 22  Changed: 22  Warnings: 0

mysql> SELECT * FROM tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸笔记本
 goods_cate: 5
 brand_name: 2
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸笔记本电脑
 goods_cate: 5
 brand_name: 7
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸游戏本
 goods_cate: 4
 brand_name: 9
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸笔记本
 goods_cate: 5
 brand_name: 2
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
*************************** 5. row ***************************
   goods_id: 5
 goods_name: X240(20ALA0EYCD) 12.5英寸超极本
 goods_cate: 7
 brand_name: 7
goods_price: 4999.000
    is_show: 1
 is_saleoff: 0
*************************** 6. row ***************************
   goods_id: 6
 goods_name: U330P 13.3英寸超极本
 goods_cate: 7
 brand_name: 7
goods_price: 4299.000
    is_show: 1
 is_saleoff: 0
*************************** 7. row ***************************
   goods_id: 7
 goods_name: SVP13226SCB 13.3英寸触控超极本
 goods_cate: 7
 brand_name: 6
goods_price: 7999.000
    is_show: 1
 is_saleoff: 0
*************************** 8. row ***************************
   goods_id: 8
 goods_name: iPad mini MD531CH/A 7.9英寸平板电脑
 goods_cate: 2
 brand_name: 8
goods_price: 1998.000
    is_show: 1
 is_saleoff: 0
*************************** 9. row ***************************
   goods_id: 9
 goods_name: iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)
 goods_cate: 2
 brand_name: 8
goods_price: 3388.000
    is_show: 1
 is_saleoff: 0
*************************** 10. row ***************************
   goods_id: 10
 goods_name:  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)
 goods_cate: 2
 brand_name: 8
goods_price: 2788.000
    is_show: 1
 is_saleoff: 0
*************************** 11. row ***************************
   goods_id: 11
 goods_name: IdeaCentre C340 20英寸一体电脑
 goods_cate: 1
 brand_name: 7
goods_price: 3499.000
    is_show: 1
 is_saleoff: 0
*************************** 12. row ***************************
   goods_id: 12
 goods_name: Vostro 3800-R1206 台式电脑
 goods_cate: 1
 brand_name: 5
goods_price: 2899.000
    is_show: 1
 is_saleoff: 0
*************************** 13. row ***************************
   goods_id: 13
 goods_name: iMac ME086CH/A 21.5英寸一体电脑
 goods_cate: 1
 brand_name: 8
goods_price: 9188.000
    is_show: 1
 is_saleoff: 0
*************************** 14. row ***************************
   goods_id: 14
 goods_name: AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )
 goods_cate: 1
 brand_name: 3
goods_price: 3699.000
    is_show: 1
 is_saleoff: 0
*************************** 15. row ***************************
   goods_id: 15
 goods_name: Z220SFF F4F06PA工作站
 goods_cate: 3
 brand_name: 4
goods_price: 4288.000
    is_show: 1
 is_saleoff: 0
*************************** 16. row ***************************
   goods_id: 16
 goods_name: PowerEdge T110 II服务器
 goods_cate: 3
 brand_name: 5
goods_price: 5388.000
    is_show: 1
 is_saleoff: 0
*************************** 17. row ***************************
   goods_id: 17
 goods_name: Mac Pro MD878CH/A 专业级台式电脑
 goods_cate: 3
 brand_name: 8
goods_price: 28888.000
    is_show: 1
 is_saleoff: 0
*************************** 18. row ***************************
   goods_id: 18
 goods_name:  HMZ-T3W 头戴显示设备
 goods_cate: 6
 brand_name: 6
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 19. row ***************************
   goods_id: 19
 goods_name: 商务双肩背包
 goods_cate: 6
 brand_name: 6
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
*************************** 20. row ***************************
   goods_id: 20
 goods_name: X3250 M4机架式服务器 2583i14
 goods_cate: 3
 brand_name: 1
goods_price: 6888.000
    is_show: 1
 is_saleoff: 0
*************************** 21. row ***************************
   goods_id: 21
 goods_name:  HMZ-T3W 头戴显示设备
 goods_cate: 6
 brand_name: 6
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 22. row ***************************
   goods_id: 22
 goods_name: 商务双肩背包
 goods_cate: 6
 brand_name: 6
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
22 rows in set (0.00 sec)

ERROR:
No query specified

mysql> SHOW COLUMNS FROM tdb_goods;
+-------------+------------------------+------+-----+---------+----------------+
| Field       | Type                   | Null | Key | Default | Extra          |
+-------------+------------------------+------+-----+---------+----------------+
| goods_id    | smallint(5) unsigned   | NO   | PRI | NULL    | auto_increment |
| goods_name  | varchar(150)           | NO   |     | NULL    |                |
| goods_cate  | varchar(40)            | NO   |     | NULL    |                |
| brand_name  | varchar(40)            | NO   |     | NULL    |                |
| goods_price | decimal(15,3) unsigned | NO   |     | 0.000   |                |
| is_show     | tinyint(1)             | NO   |     | 1       |                |
| is_saleoff  | tinyint(1)             | NO   |     | 0       |                |
+-------------+------------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> ALTER TABLE tdb_goods
    -> CHANGE goods_cate cate_id SMALLINT UNSIGNED NOT NULL,
    -> CHANGE brand_name brand_id SMALLINT UNSIGNED NOT NULL;
Query OK, 22 rows affected (0.67 sec)
Records: 22  Duplicates: 0  Warnings: 0

mysql> SHOW COLUMNS FROM tdb_goods;
+-------------+------------------------+------+-----+---------+----------------+
| Field       | Type                   | Null | Key | Default | Extra          |
+-------------+------------------------+------+-----+---------+----------------+
| goods_id    | smallint(5) unsigned   | NO   | PRI | NULL    | auto_increment |
| goods_name  | varchar(150)           | NO   |     | NULL    |                |
| cate_id     | smallint(5) unsigned   | NO   |     | NULL    |                |
| brand_id    | smallint(5) unsigned   | NO   |     | NULL    |                |
| goods_price | decimal(15,3) unsigned | NO   |     | 0.000   |                |
| is_show     | tinyint(1)             | NO   |     | 1       |                |
| is_saleoff  | tinyint(1)             | NO   |     | 0       |                |
+-------------+------------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> #创建一个事实的外键;
mysql> SELECT * FROM tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸笔记本
    cate_id: 5
   brand_id: 2
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸笔记本电脑
    cate_id: 5
   brand_id: 7
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸游戏本
    cate_id: 4
   brand_id: 9
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸笔记本
    cate_id: 5
   brand_id: 2
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
*************************** 5. row ***************************
   goods_id: 5
 goods_name: X240(20ALA0EYCD) 12.5英寸超极本
    cate_id: 7
   brand_id: 7
goods_price: 4999.000
    is_show: 1
 is_saleoff: 0
*************************** 6. row ***************************
   goods_id: 6
 goods_name: U330P 13.3英寸超极本
    cate_id: 7
   brand_id: 7
goods_price: 4299.000
    is_show: 1
 is_saleoff: 0
*************************** 7. row ***************************
   goods_id: 7
 goods_name: SVP13226SCB 13.3英寸触控超极本
    cate_id: 7
   brand_id: 6
goods_price: 7999.000
    is_show: 1
 is_saleoff: 0
*************************** 8. row ***************************
   goods_id: 8
 goods_name: iPad mini MD531CH/A 7.9英寸平板电脑
    cate_id: 2
   brand_id: 8
goods_price: 1998.000
    is_show: 1
 is_saleoff: 0
*************************** 9. row ***************************
   goods_id: 9
 goods_name: iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)
    cate_id: 2
   brand_id: 8
goods_price: 3388.000
    is_show: 1
 is_saleoff: 0
*************************** 10. row ***************************
   goods_id: 10
 goods_name:  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)
    cate_id: 2
   brand_id: 8
goods_price: 2788.000
    is_show: 1
 is_saleoff: 0
*************************** 11. row ***************************
   goods_id: 11
 goods_name: IdeaCentre C340 20英寸一体电脑
    cate_id: 1
   brand_id: 7
goods_price: 3499.000
    is_show: 1
 is_saleoff: 0
*************************** 12. row ***************************
   goods_id: 12
 goods_name: Vostro 3800-R1206 台式电脑
    cate_id: 1
   brand_id: 5
goods_price: 2899.000
    is_show: 1
 is_saleoff: 0
*************************** 13. row ***************************
   goods_id: 13
 goods_name: iMac ME086CH/A 21.5英寸一体电脑
    cate_id: 1
   brand_id: 8
goods_price: 9188.000
    is_show: 1
 is_saleoff: 0
*************************** 14. row ***************************
   goods_id: 14
 goods_name: AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )
    cate_id: 1
   brand_id: 3
goods_price: 3699.000
    is_show: 1
 is_saleoff: 0
*************************** 15. row ***************************
   goods_id: 15
 goods_name: Z220SFF F4F06PA工作站
    cate_id: 3
   brand_id: 4
goods_price: 4288.000
    is_show: 1
 is_saleoff: 0
*************************** 16. row ***************************
   goods_id: 16
 goods_name: PowerEdge T110 II服务器
    cate_id: 3
   brand_id: 5
goods_price: 5388.000
    is_show: 1
 is_saleoff: 0
*************************** 17. row ***************************
   goods_id: 17
 goods_name: Mac Pro MD878CH/A 专业级台式电脑
    cate_id: 3
   brand_id: 8
goods_price: 28888.000
    is_show: 1
 is_saleoff: 0
*************************** 18. row ***************************
   goods_id: 18
 goods_name:  HMZ-T3W 头戴显示设备
    cate_id: 6
   brand_id: 6
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 19. row ***************************
   goods_id: 19
 goods_name: 商务双肩背包
    cate_id: 6
   brand_id: 6
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
*************************** 20. row ***************************
   goods_id: 20
 goods_name: X3250 M4机架式服务器 2583i14
    cate_id: 3
   brand_id: 1
goods_price: 6888.000
    is_show: 1
 is_saleoff: 0
*************************** 21. row ***************************
   goods_id: 21
 goods_name:  HMZ-T3W 头戴显示设备
    cate_id: 6
   brand_id: 6
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 22. row ***************************
   goods_id: 22
 goods_name: 商务双肩背包
    cate_id: 6
   brand_id: 6
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
22 rows in set (0.00 sec)

ERROR:
No query specified

mysql> INSERT tdb_goods_cates(cate_name) VALUES('路由器'),('交换机'),('网卡');
ERROR 1146 (42S02): Table 'world.tdb_goods_cates' doesn't exist
mysql>
mysql>    INSERT tdb_goods_brands(brand_name) VALUES('海尔'),('清华同方'),('神舟');
Query OK, 3 rows affected (0.07 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> #在tdb_goods数据表写入任意记录
mysql> INSERT tdb_goods(goods_name,cate_id,brand_id,goods_price) 
VALUES(' LaserJet Pro P1606dn 黑白激光打印机','12','4','1849');
Query OK, 1 row affected (0.08 sec)

mysql> SELECT * FROM tdb_goods_cate;
+---------+---------------+
| cate_id | cate_name     |
+---------+---------------+
|       1 | 台式机        |
|       2 | 平板电脑      |
|       3 | 服务器/工作站 |
|       4 | 游戏本        |
|       5 | 笔记本        |
|       6 | 笔记本配件    |
|       7 | 超级本        |
+---------+---------------+
7 rows in set (0.03 sec)

mysql> SELECT * FROM tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸笔记本
    cate_id: 5
   brand_id: 2
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸笔记本电脑
    cate_id: 5
   brand_id: 7
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸游戏本
    cate_id: 4
   brand_id: 9
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸笔记本
    cate_id: 5
   brand_id: 2
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
*************************** 5. row ***************************
   goods_id: 5
 goods_name: X240(20ALA0EYCD) 12.5英寸超极本
    cate_id: 7
   brand_id: 7
goods_price: 4999.000
    is_show: 1
 is_saleoff: 0
*************************** 6. row ***************************
   goods_id: 6
 goods_name: U330P 13.3英寸超极本
    cate_id: 7
   brand_id: 7
goods_price: 4299.000
    is_show: 1
 is_saleoff: 0
*************************** 7. row ***************************
   goods_id: 7
 goods_name: SVP13226SCB 13.3英寸触控超极本
    cate_id: 7
   brand_id: 6
goods_price: 7999.000
    is_show: 1
 is_saleoff: 0
*************************** 8. row ***************************
   goods_id: 8
 goods_name: iPad mini MD531CH/A 7.9英寸平板电脑
    cate_id: 2
   brand_id: 8
goods_price: 1998.000
    is_show: 1
 is_saleoff: 0
*************************** 9. row ***************************
   goods_id: 9
 goods_name: iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)
    cate_id: 2
   brand_id: 8
goods_price: 3388.000
    is_show: 1
 is_saleoff: 0
*************************** 10. row ***************************
   goods_id: 10
 goods_name:  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)
    cate_id: 2
   brand_id: 8
goods_price: 2788.000
    is_show: 1
 is_saleoff: 0
*************************** 11. row ***************************
   goods_id: 11
 goods_name: IdeaCentre C340 20英寸一体电脑
    cate_id: 1
   brand_id: 7
goods_price: 3499.000
    is_show: 1
 is_saleoff: 0
*************************** 12. row ***************************
   goods_id: 12
 goods_name: Vostro 3800-R1206 台式电脑
    cate_id: 1
   brand_id: 5
goods_price: 2899.000
    is_show: 1
 is_saleoff: 0
*************************** 13. row ***************************
   goods_id: 13
 goods_name: iMac ME086CH/A 21.5英寸一体电脑
    cate_id: 1
   brand_id: 8
goods_price: 9188.000
    is_show: 1
 is_saleoff: 0
*************************** 14. row ***************************
   goods_id: 14
 goods_name: AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )
    cate_id: 1
   brand_id: 3
goods_price: 3699.000
    is_show: 1
 is_saleoff: 0
*************************** 15. row ***************************
   goods_id: 15
 goods_name: Z220SFF F4F06PA工作站
    cate_id: 3
   brand_id: 4
goods_price: 4288.000
    is_show: 1
 is_saleoff: 0
*************************** 16. row ***************************
   goods_id: 16
 goods_name: PowerEdge T110 II服务器
    cate_id: 3
   brand_id: 5
goods_price: 5388.000
    is_show: 1
 is_saleoff: 0
*************************** 17. row ***************************
   goods_id: 17
 goods_name: Mac Pro MD878CH/A 专业级台式电脑
    cate_id: 3
   brand_id: 8
goods_price: 28888.000
    is_show: 1
 is_saleoff: 0
*************************** 18. row ***************************
   goods_id: 18
 goods_name:  HMZ-T3W 头戴显示设备
    cate_id: 6
   brand_id: 6
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 19. row ***************************
   goods_id: 19
 goods_name: 商务双肩背包
    cate_id: 6
   brand_id: 6
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
*************************** 20. row ***************************
   goods_id: 20
 goods_name: X3250 M4机架式服务器 2583i14
    cate_id: 3
   brand_id: 1
goods_price: 6888.000
    is_show: 1
 is_saleoff: 0
*************************** 21. row ***************************
   goods_id: 21
 goods_name:  HMZ-T3W 头戴显示设备
    cate_id: 6
   brand_id: 6
goods_price: 6999.000
    is_show: 1
 is_saleoff: 0
*************************** 22. row ***************************
   goods_id: 22
 goods_name: 商务双肩背包
    cate_id: 6
   brand_id: 6
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
*************************** 23. row ***************************
   goods_id: 23
 goods_name:  LaserJet Pro P1606dn 黑白激光打印机
    cate_id: 12
   brand_id: 4
goods_price: 1849.000
    is_show: 1
 is_saleoff: 0
23 rows in set (0.00 sec)

ERROR:
No query specified

mysql> #数据表参照,用别名;
mysql> #内链接 ,取交集;
mysql> SHOW COLUMNS FROM tdb_goods;
+-------------+------------------------+------+-----+---------+----------------+
| Field       | Type                   | Null | Key | Default | Extra   |
+-------------+------------------------+------+-----+---------+----------------+
| goods_id    | smallint(5) unsigned   | NO   | PRI | NULL    | auto_increment |
| goods_name  | varchar(150)           | NO   |     | NULL    |       |
| cate_id     | smallint(5) unsigned   | NO   |     | NULL    |           |
| brand_id    | smallint(5) unsigned   | NO   |     | NULL    |           |
| goods_price | decimal(15,3) unsigned | NO   |     | 0.000   |              |
| is_show     | tinyint(1)             | NO   |     | 1       |     |
| is_saleoff  | tinyint(1)             | NO   |     | 0       |      |
+-------------+------------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM tdb_goods_cate;
+-----------+----------------------+------+-----+---------+----------------+
| Field     | Type                 | Null | Key | Default | Extra       |
+-----------+----------------------+------+-----+---------+----------------+
| cate_id   | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| cate_name | varchar(40)          | NO   |     | NULL    |            |
+-----------+----------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> SELECT goods_id,goods_name,cate_name
    -> FROM tdb_goods INNER JOIN tdb_goods_cate
    -> ON tdb_goods.cate_id=tdb_goods_cate.cate_id;
+----------+------------------------------------------------------------------------+---------------+
| goods_id | goods_name                               | cate_name     |
+----------+------------------------------------------------------------------------+---------------+
|        1 | R510VC 15.6英寸笔记本                    | 笔记本        |
|        2 | Y400N 14.0英寸笔记本电脑                | 笔记本        |
|        3 | G150TH 15.6英寸游戏本                 | 游戏本        |
|        4 | X550CC 15.6英寸笔记本                   | 笔记本        |
|        5 | X240(20ALA0EYCD) 12.5英寸超极本               | 超级本        |
|        6 | U330P 13.3英寸超极本                     | 超级本        |
|        7 | SVP13226SCB 13.3英寸触控超极本                                         | 超级本        |
|        8 | iPad mini MD531CH/A 7.9英寸平板电脑                                    | 平板电脑      |
|        9 | iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)                      | 平板电脑      |
|       10 |  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版) | 平板电脑      |
|       11 | IdeaCentre C340 20英寸一体电脑                                         | 台式机        |
|       12 | Vostro 3800-R1206 台式电脑                                             | 台式机        |
|       13 | iMac ME086CH/A 21.5英寸一体电脑                                        | 台式机        |
|       14 | AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )     | 台式机        |
|       15 | Z220SFF F4F06PA工作站                                                  | 服务器/工作站 |
|       16 | PowerEdge T110 II服务器                                                | 服务器/工作站 |
|       17 | Mac Pro MD878CH/A 专业级台式电脑                                       | 服务器/工作站 |
|       18 |  HMZ-T3W 头戴显示设备                                                  | 笔记本配件    |
|       19 | 商务双肩背包                                                           | 笔记本配件    |
|       20 | X3250 M4机架式服务器 2583i14                                           | 服务器/工作站 |
|       21 |  HMZ-T3W 头戴显示设备                                                  | 笔记本配件    |
|       22 | 商务双肩背包                                                           | 笔记本配件    |
+----------+------------------------------------------------------------------------+---------------+
22 rows in set (0.01 sec)

mysql> SELECT * FROM tdb_goods_cate;
+---------+---------------+
| cate_id | cate_name     |
+---------+---------------+
|       1 | 台式机        |
|       2 | 平板电脑      |
|       3 | 服务器/工作站 |
|       4 | 游戏本        |
|       5 | 笔记本        |
|       6 | 笔记本配件    |
|       7 | 超级本        |
+---------+---------------+
7 rows in set (0.00 sec)

mysql> #左外连接,取左表中的全部和右表符合条件的部分;
mysql> SELECT goods_id,goods_name,cate_name
    -> FROM tdb_goods LEFT JOIN tdb_goods_cate
    -> ON tdb_goods.cate_id=tdb_goods_cate.cate_id;
+----------+------------------------------------------------------------------------+---------------+
| goods_id | goods_name                                           | cate_name     |
+----------+------------------------------------------------------------------------+---------------+
|        1 | R510VC 15.6英寸笔记本                                 | 笔记本        |
|        2 | Y400N 14.0英寸笔记本电脑                                | 笔记本        |
|        3 | G150TH 15.6英寸游戏本                                 | 游戏本        |
|        4 | X550CC 15.6英寸笔记本                                 | 笔记本        |
|        5 | X240(20ALA0EYCD) 12.5英寸超极本                          | 超级本        |
|        6 | U330P 13.3英寸超极本                                 | 超级本        |
|        7 | SVP13226SCB 13.3英寸触控超极本                           | 超级本        |
|        8 | iPad mini MD531CH/A 7.9英寸平板电脑                        | 平板电脑      |
|        9 | iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)                | 平板电脑      |
|       10 |  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版) | 平板电脑      |
|       11 | IdeaCentre C340 20英寸一体电脑                            | 台式机        |
|       12 | Vostro 3800-R1206 台式电脑                               | 台式机        |
|       13 | iMac ME086CH/A 21.5英寸一体电脑                          | 台式机        |
|       14 | AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )     | 台式机        |
|       15 | Z220SFF F4F06PA工作站                                | 服务器/工作站 |
|       16 | PowerEdge T110 II服务器                               | 服务器/工作站 |
|       17 | Mac Pro MD878CH/A 专业级台式电脑                          | 服务器/工作站 |
|       18 |  HMZ-T3W 头戴显示设备                               | 笔记本配件    |
|       19 | 商务双肩背包                                         | 笔记本配件    |
|       20 | X3250 M4机架式服务器 2583i14                              | 服务器/工作站 |
|       21 |  HMZ-T3W 头戴显示设备                                  | 笔记本配件    |
|       22 | 商务双肩背包                                         | 笔记本配件    |
|       23 |  LaserJet Pro P1606dn 黑白激光打印机                       | NULL          |
+----------+------------------------------------------------------------------------+---------------+
23 rows in set (0.00 sec)

mysql> #右外连接,取右表中的全部和左表符合条件的部分;
mysql> SELECT goods_id,goods_name,cate_name
    -> FROM tdb_goods RIGHT JOIN tdb_goods_cate
    -> ON tdb_goods.cate_id=tdb_goods_cate.cate_id;
+----------+------------------------------------------------------------------------+---------------+
| goods_id | goods_name                                             | cate_name     |
+----------+------------------------------------------------------------------------+---------------+
|        1 | R510VC 15.6英寸笔记本                                | 笔记本        |
|        2 | Y400N 14.0英寸笔记本电脑                                               | 笔记本        |
|        3 | G150TH 15.6英寸游戏本                                                  | 游戏本        |
|        4 | X550CC 15.6英寸笔记本                                                  | 笔记本        |
|        5 | X240(20ALA0EYCD) 12.5英寸超极本                                        | 超级本        |
|        6 | U330P 13.3英寸超极本                                                   | 超级本        |
|        7 | SVP13226SCB 13.3英寸触控超极本                                         | 超级本        |
|        8 | iPad mini MD531CH/A 7.9英寸平板电脑                                    | 平板电脑      |
|        9 | iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)                      | 平板电脑      |
|       10 |  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版) | 平板电脑      |
|       11 | IdeaCentre C340 20英寸一体电脑                                         | 台式机        |
|       12 | Vostro 3800-R1206 台式电脑                                             | 台式机        |
|       13 | iMac ME086CH/A 21.5英寸一体电脑                                        | 台式机        |
|       14 | AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )     | 台式机        |
|       15 | Z220SFF F4F06PA工作站                                                  | 服务器/工作站 |
|       16 | PowerEdge T110 II服务器                                                | 服务器/工作站 |
|       17 | Mac Pro MD878CH/A 专业级台式电脑                                       | 服务器/工作站 |
|       18 |  HMZ-T3W 头戴显示设备                                                  | 笔记本配件    |
|       19 | 商务双肩背包                                                           | 笔记本配件    |
|       20 | X3250 M4机架式服务器 2583i14                                           | 服务器/工作站 |
|       21 |  HMZ-T3W 头戴显示设备                                                  | 笔记本配件    |
|       22 | 商务双肩背包                                                           | 笔记本配件    |
+----------+------------------------------------------------------------------------+---------------+
22 rows in set (0.02 sec)

mysql> SELECT goods_id,goods_name,cate_name
    -> FROM tdb_goods LEFT JOIN tdb_goods_cate
    -> ON tdb_goods.cate_id=tdb_goods_cate.cate_id\G;
*************************** 1. row ***************************
  goods_id: 1
goods_name: R510VC 15.6英寸笔记本
 cate_name: 笔记本
*************************** 2. row ***************************
  goods_id: 2
goods_name: Y400N 14.0英寸笔记本电脑
 cate_name: 笔记本
*************************** 3. row ***************************
  goods_id: 3
goods_name: G150TH 15.6英寸游戏本
 cate_name: 游戏本
*************************** 4. row ***************************
  goods_id: 4
goods_name: X550CC 15.6英寸笔记本
 cate_name: 笔记本
*************************** 5. row ***************************
  goods_id: 5
goods_name: X240(20ALA0EYCD) 12.5英寸超极本
 cate_name: 超级本
*************************** 6. row ***************************
  goods_id: 6
goods_name: U330P 13.3英寸超极本
 cate_name: 超级本
*************************** 7. row ***************************
  goods_id: 7
goods_name: SVP13226SCB 13.3英寸触控超极本
 cate_name: 超级本
*************************** 8. row ***************************
  goods_id: 8
goods_name: iPad mini MD531CH/A 7.9英寸平板电脑
 cate_name: 平板电脑
*************************** 9. row ***************************
  goods_id: 9
goods_name: iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)
 cate_name: 平板电脑
*************************** 10. row ***************************
  goods_id: 10
goods_name:  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)
 cate_name: 平板电脑
*************************** 11. row ***************************
  goods_id: 11
goods_name: IdeaCentre C340 20英寸一体电脑
 cate_name: 台式机
*************************** 12. row ***************************
  goods_id: 12
goods_name: Vostro 3800-R1206 台式电脑
 cate_name: 台式机
*************************** 13. row ***************************
  goods_id: 13
goods_name: iMac ME086CH/A 21.5英寸一体电脑
 cate_name: 台式机
*************************** 14. row ***************************
  goods_id: 14
goods_name: AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )
 cate_name: 台式机
*************************** 15. row ***************************
  goods_id: 15
goods_name: Z220SFF F4F06PA工作站
 cate_name: 服务器/工作站
*************************** 16. row ***************************
  goods_id: 16
goods_name: PowerEdge T110 II服务器
 cate_name: 服务器/工作站
*************************** 17. row ***************************
  goods_id: 17
goods_name: Mac Pro MD878CH/A 专业级台式电脑
 cate_name: 服务器/工作站
*************************** 18. row ***************************
  goods_id: 18
goods_name:  HMZ-T3W 头戴显示设备
 cate_name: 笔记本配件
*************************** 19. row ***************************
  goods_id: 19
goods_name: 商务双肩背包
 cate_name: 笔记本配件
*************************** 20. row ***************************
  goods_id: 20
goods_name: X3250 M4机架式服务器 2583i14
 cate_name: 服务器/工作站
*************************** 21. row ***************************
  goods_id: 21
goods_name:  HMZ-T3W 头戴显示设备
 cate_name: 笔记本配件
*************************** 22. row ***************************
  goods_id: 22
goods_name: 商务双肩背包
 cate_name: 笔记本配件
*************************** 23. row ***************************
  goods_id: 23
goods_name:  LaserJet Pro P1606dn 黑白激光打印机
 cate_name: NULL
23 rows in set (0.00 sec)

ERROR:
No query specified

mysql> #多表连接;
mysql> SHOW COLUMNS FROM tdb_goods\G;
*************************** 1. row ***************************
  Field: goods_id
   Type: smallint(5) unsigned
   Null: NO
    Key: PRI
Default: NULL
  Extra: auto_increment
*************************** 2. row ***************************
  Field: goods_name
   Type: varchar(150)
   Null: NO
    Key:
Default: NULL
  Extra:
*************************** 3. row ***************************
  Field: cate_id
   Type: smallint(5) unsigned
   Null: NO
    Key:
Default: NULL
  Extra:
*************************** 4. row ***************************
  Field: brand_id
   Type: smallint(5) unsigned
   Null: NO
    Key:
Default: NULL
  Extra:
*************************** 5. row ***************************
  Field: goods_price
   Type: decimal(15,3) unsigned
   Null: NO
    Key:
Default: 0.000
  Extra:
*************************** 6. row ***************************
  Field: is_show
   Type: tinyint(1)
   Null: NO
    Key:
Default: 1
  Extra:
*************************** 7. row ***************************
  Field: is_saleoff
   Type: tinyint(1)
   Null: NO
    Key:
Default: 0
  Extra:
7 rows in set (0.00 sec)

ERROR:
No query specified

mysql> #实现三张表的连接;

mysql> SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g
    -> INNER JOIN tdb_goods_cate AS c
    -> ON g.cate_id=c.cate_id
    -> INNER JOIN tdb_goods_brands AS b
    -> ON g.brand_id=b.brand_id\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸笔记本
  cate_name: 笔记本
 brand_name: 华硕
goods_price: 3399.000
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸笔记本电脑
  cate_name: 笔记本
 brand_name: 联想
goods_price: 4899.000
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸游戏本
  cate_name: 游戏本
 brand_name: 雷神
goods_price: 8499.000
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸笔记本
  cate_name: 笔记本
 brand_name: 华硕
goods_price: 2799.000
*************************** 5. row ***************************
   goods_id: 5
 goods_name: X240(20ALA0EYCD) 12.5英寸超极本
  cate_name: 超级本
 brand_name: 联想
goods_price: 4999.000
*************************** 6. row ***************************
   goods_id: 6
 goods_name: U330P 13.3英寸超极本
  cate_name: 超级本
 brand_name: 联想
goods_price: 4299.000
*************************** 7. row ***************************
   goods_id: 7
 goods_name: SVP13226SCB 13.3英寸触控超极本
  cate_name: 超级本
 brand_name: 索尼
goods_price: 7999.000
*************************** 8. row ***************************
   goods_id: 8
 goods_name: iPad mini MD531CH/A 7.9英寸平板电脑
  cate_name: 平板电脑
 brand_name: 苹果
goods_price: 1998.000
*************************** 9. row ***************************
   goods_id: 9
 goods_name: iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)
  cate_name: 平板电脑
 brand_name: 苹果
goods_price: 3388.000
*************************** 10. row ***************************
   goods_id: 10
 goods_name:  iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)
  cate_name: 平板电脑
 brand_name: 苹果
goods_price: 2788.000
*************************** 11. row ***************************
   goods_id: 11
 goods_name: IdeaCentre C340 20英寸一体电脑
  cate_name: 台式机
 brand_name: 联想
goods_price: 3499.000
*************************** 12. row ***************************
   goods_id: 12
 goods_name: Vostro 3800-R1206 台式电脑
  cate_name: 台式机
 brand_name: 戴尔
goods_price: 2899.000
*************************** 13. row ***************************
   goods_id: 13
 goods_name: iMac ME086CH/A 21.5英寸一体电脑
  cate_name: 台式机
 brand_name: 苹果
goods_price: 9188.000
*************************** 14. row ***************************
   goods_id: 14
 goods_name: AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )
  cate_name: 台式机
 brand_name: 宏碁
goods_price: 3699.000
*************************** 15. row ***************************
   goods_id: 15
 goods_name: Z220SFF F4F06PA工作站
  cate_name: 服务器/工作站
 brand_name: 惠普
goods_price: 4288.000
*************************** 16. row ***************************
   goods_id: 16
 goods_name: PowerEdge T110 II服务器
  cate_name: 服务器/工作站
 brand_name: 戴尔
goods_price: 5388.000
*************************** 17. row ***************************
   goods_id: 17
 goods_name: Mac Pro MD878CH/A 专业级台式电脑
  cate_name: 服务器/工作站
 brand_name: 苹果
goods_price: 28888.000
*************************** 18. row ***************************
   goods_id: 18
 goods_name:  HMZ-T3W 头戴显示设备
  cate_name: 笔记本配件
 brand_name: 索尼
goods_price: 6999.000
*************************** 19. row ***************************
   goods_id: 19
 goods_name: 商务双肩背包
  cate_name: 笔记本配件
 brand_name: 索尼
goods_price: 99.000
*************************** 20. row ***************************
   goods_id: 20
 goods_name: X3250 M4机架式服务器 2583i14
  cate_name: 服务器/工作站
 brand_name: IBM
goods_price: 6888.000
*************************** 21. row ***************************
   goods_id: 21
 goods_name:  HMZ-T3W 头戴显示设备
  cate_name: 笔记本配件
 brand_name: 索尼
goods_price: 6999.000
*************************** 22. row ***************************
   goods_id: 22
 goods_name: 商务双肩背包
  cate_name: 笔记本配件
 brand_name: 索尼
goods_price: 99.000
22 rows in set (0.02 sec)

ERROR:
No query specified

mysql>
mysql> #SHOW COLUMNS FROM tdb_goods_cate;
mysql> SHOW COLUMNS FROM tdb_goods_cate;
+-----------+----------------------+------+-----+---------+----------------+
| Field     | Type                 | Null | Key | Default | Extra          |
+-----------+----------------------+------+-----+---------+----------------+
| cate_id   | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| cate_name | varchar(40)          | NO   |     | NULL    |                |
+-----------+----------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM tdb_goods_cate;
+---------+---------------+
| cate_id | cate_name     |
+---------+---------------+
|       1 | 台式机        |
|       2 | 平板电脑      |
|       3 | 服务器/工作站 |
|       4 | 游戏本        |
|       5 | 笔记本        |
|       6 | 笔记本配件    |
|       7 | 超级本        |
+---------+---------------+
7 rows in set (0.00 sec)

mysql> CREATE TABLE tdb_goods_types(
    ->      type_id   SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    ->      type_name VARCHAR(20) NOT NULL,
    ->      parent_id SMALLINT UNSIGNED NOT NULL DEFAULT 0
    ->   );
Query OK, 0 rows affected (0.19 sec)

mysql> INSERT tdb_goods_types(type_name,parent_id) VALUES('家用电器',DEFAULT);
Query OK, 1 row affected (0.05 sec)

mysql>   INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑、办公',DEFAULT);
Query OK, 1 row affected (0.02 sec)

mysql>   INSERT tdb_goods_types(type_name,parent_id) VALUES('大家电',1);
Query OK, 1 row affected (0.06 sec)

mysql>   INSERT tdb_goods_types(type_name,parent_id) VALUES('生活电器',1);
Query OK, 1 row affected (0.05 sec)

mysql>   INSERT tdb_goods_types(type_name,parent_id) VALUES('平板电视',3);
Query OK, 1 row affected (0.02 sec)

mysql>   INSERT tdb_g                    

人气教程排行