连接查询:
1、连接查询分为内连接和外连接,其中外连接又分为左连接和右连接。
2、内连接:使用比较运算符进行表面某列数据的的比较操作,并列出这些表中与连接条件相匹配的数据行。只返回满足条件的记录。
3、外连接:返回的结果不仅包括符合条件的行,还包括不符合条件的行
(左外连接):返回包括左表中的所有记录和游标中连接字段相等的记录。结果包括LEFT OUTER子句中指定的左表所有行。
(右外连接):返回包括右表中的所有记录和左表中连接字段相等的记录。是左连接的反向连接,将返回右表的所有行。
1、准备数据表
#创建名为suppliers的数据表
MariaDB [vincen]> CREATE TABLE suppliers
-> (
-> s_id int NOT NULL AUTO_INCREMENT,
-> s_name char(50) NOT NULL,
-> s_city char(50) NULL,
-> s_zip char(10) NULL,
-> s_call CHAR(50) NOT NULL,
-> PRIMARY KEY (s_id)
-> ) ;
Query OK, 0 rows affected (0.02 sec)
#为suppliers数据表每个相应的字段插入数据
MariaDB [vincen]> INSERT INTO suppliers(s_id, s_name,s_city, s_zip, s_call)
-> VALUES(101,'FastFruit Inc.','Tianjin','300000','48075'),
-> (102,'LT Supplies','Chongqing','400000','44333'),
-> (103,'ACME','Shanghai','200000','90046'),
-> (104,'FNK Inc.','Zhongshan','528437','11111'),
-> (105,'Good Set','Taiyuang','030000', '22222'),
-> (106,'Just Eat Ours','Beijing','010', '45678'),
-> (107,'DK Inc.','Zhengzhou','450000', '33332');
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
#创建名为orders的数据表
MariaDB [vincen]> CREATE TABLE orders
-> (
-> o_num int NOT NULL AUTO_INCREMENT,
-> o_date datetime NOT NULL,
-> c_id int NOT NULL,
-> PRIMARY KEY (o_num)
-> ) ;
Query OK, 0 rows affected (0.02 sec)
#给orders数据表中每个字段插入数据
MariaDB [vincen]> INSERT INTO orders(o_num, o_date, c_id)
-> VALUES(30001, '2008-09-01', 10001),
-> (30002, '2008-09-12', 10003),
-> (30003, '2008-09-30', 10004),
-> (30004, '2008-10-03', 10005),
-> (30005, '2008-10-08', 10001);
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
#创建名为customers的数据表
MariaDB [vincen]> CREATE TABLE customers
-> (
-> c_id int NOT NULL AUTO_INCREMENT,
-> c_name char(50) NOT NULL,
-> c_address char(50) NULL,
-> c_city char(50) NULL,
-> c_zip char(10) NULL,
-> c_contact char(50) NULL,
-> c_email char(255) NULL,
-> PRIMARY KEY (c_id)
-> );
Query OK, 0 rows affected (0.01 sec)
#给customers数据表中每个字段插入数据
MariaDB [vincen]> INSERT INTO customers(c_id, c_name, c_address, c_city,
-> c_zip, c_contact, c_email)
-> VALUES(10001, 'RedHook', '200 Street ', 'Tianjin',
-> '300000', 'LiMing', '[email protected]'),
-> (10002, 'Stars', '333 Fromage Lane',
-> 'Dalian', '116000', 'Zhangbo','[email protected]'),
-> (10003, 'Netbhood', '1 Sunny Place', 'Qingdao', '266000',
-> 'LuoCong', NULL),
-> (10004, 'JOTO', '829 Riverside Drive', 'Haikou',
-> '570000', 'YangShan', '[email protected]');
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
2、查看两个数据表的结构
MariaDB [vincen]> desc fruits;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| f_id | char(10) | NO | PRI | NULL | |
| s_id | int(11) | NO | | NULL | |
| f_name | char(255) | NO | | NULL | |
| f_price | decimal(8,2) | NO | | NULL | |
+---------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
MariaDB [vincen]> desc suppliers;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| s_id | int(11) | NO | PRI | NULL | auto_increment |
| s_name | char(50) | NO | | NULL | |
| s_city | char(50) | YES | | NULL | |
| s_zip | char(10) | YES | | NULL | |
| s_call | char(50) | NO | | NULL | |
+--------+----------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
3、使用INNER JOIN内连接
#在fruits表和suppliers表中使用内连接查询
MariaDB [vincen]> SELECT suppliers.s_id,s_name #查询suppliers表中的s_id,s_name字段
-> f_name,f_price #查询fruits表中的f_name,f_price字段
-> FROM fruits INNER JOIN suppliers #从fruits内连接suppliers的表中
-> ON fruits.s_id = suppliers.s_id; #条件是fruits表中的s_id字段和suppliers表中的s_id字段相等
+------+----------------+------------+---------+
| s_id | s_name | f_name | f_price |
+------+----------------+------------+---------+
| 101 | FastFruit Inc. | apple | 5.20 |
| 103 | ACME | apricot | 2.20 |
| 101 | FastFruit Inc. | blackberry | 10.20 |
| 104 | FNK Inc. | berry | 7.60 |
| 107 | DK Inc. | xxxx | 3.60 |
| 102 | LT Supplies | orange | 11.20 |
| 105 | Good Set | melon | 8.20 |
| 101 | FastFruit Inc. | cherry | 3.20 |
| 104 | FNK Inc. | lemon | 6.40 |
| 106 | Just Eat Ours | mango | 15.60 |
| 105 | Good Set | xbabay | 2.60 |
| 105 | Good Set | xxtt | 11.60 |
| 103 | ACME | coconut | 9.20 |
| 102 | LT Supplies | banana | 10.30 |
| 102 | LT Supplies | grape | 5.30 |
| 107 | DK Inc. | xbababa | 3.60 |
+------+----------------+------------+---------+
16 rows in set (0.01 sec)
4、左外连接查询(LEFT JOIN)
#在customers表和orders表中,查询所有客户,包括没有订单的用户
MariaDB [vincen]> SELECT customers.c_id, #查询customers表中的c_id字段
-> orders.o_num #查询orders表中的o_num字段
-> FROM customers LEFT OUTER JOIN orders #customers作为左表外连接右表orders
-> ON #条件是
-> customers.c_id = orders.c_id; #customers表中的c_id字段和orders表中的c_id字段相等
+-------+-------+
| c_id | o_num |
+-------+-------+
| 10001 | 30001 |
| 10003 | 30002 |
| 10004 | 30003 |
| 10001 | 30005 |
| 10002 | NULL | #ID等于1002的客户并没有下单,所以对应的orders表中没有该用户的信息
+-------+-------+
5 rows in set (0.02 sec)
5、右外连接查询(RIGHT JOIN)
#在customers表和orders表中,查询所有订单,包括没有客户的订单
MariaDB [vincen]> SELECT customers.c_id, #查询customers表中的c_id字段
-> orders.o_num #查询orders表中的o_num字段
-> FROM customers RIGHT OUTER JOIN orders #customers表作为右表外连接orders表
-> ON #条件是
-> customers.c_id = orders.c_id; #customers表中的c_id字段和orders表中的c_id字段相等
+-------+-------+
| c_id | o_num |
+-------+-------+
| 10001 | 30001 |
| 10003 | 30002 |
| 10004 | 30003 |
| NULL | 30004 |
| 10001 | 30005 |
+-------+-------+
5 rows in set (0.01 sec)
6、复合条件连接查询
在连接查询的过程中,通过添加过滤条件,限制查询的结果,使查询的结果更加准确。
#在fruits表和suppliers表之间,使用INNER JOIN语法进行内连接查询,并对结果排序
MariaDB [vincen]> SELECT suppliers.s_id, #查询suppliers表中的s_id字段
-> s_name, #查询suppliers表中的s_name字段
-> f_name, f_price #查询fruits表中的f_name和f_price字段
-> FROM fruits INNER JOIN suppliers #fruits表内连接suppliers表
-> ON #条件是
-> fruits.s_id = suppliers.s_id #fruits表中的s_id字段和suppliers表中的s_id字段相等
-> ORDER BY fruits.s_id; #根据fruits表中的s_id字段排序
+------+----------------+------------+---------+
| s_id | s_name | f_name | f_price |
+------+----------------+------------+---------+
| 101 | FastFruit Inc. | apple | 5.20 |
| 101 | FastFruit Inc. | blackberry | 10.20 |
| 101 | FastFruit Inc. | cherry | 3.20 |
| 102 | LT Supplies | grape | 5.30 |
| 102 | LT Supplies | banana | 10.30 |
| 102 | LT Supplies | orange | 11.20 |
| 103 | ACME | apricot | 2.20 |
| 103 | ACME | coconut | 9.20 |
| 104 | FNK Inc. | lemon | 6.40 |
| 104 | FNK Inc. | berry | 7.60 |
| 105 | Good Set | xbabay | 2.60 |
| 105 | Good Set | xxtt | 11.60 |
| 105 | Good Set | melon | 8.20 |
| 106 | Just Eat Ours | mango | 15.60 |
| 107 | DK Inc. | xxxx | 3.60 |
| 107 | DK Inc. | xbababa | 3.60 |
+------+----------------+------------+---------+
16 rows in set (0.01 sec)