文章目录

  • 一、子查询与表连接(子查询的使用)
    • ⼦查询(嵌套sql)
    • 利⽤⼦查询进⾏过滤
  • 二、子查询与表连接 (作为计算字段使用子查询)
  • 三、表关系(什么是关系表)
  • 四、表关系(一对一关系与外键)
  • 五、表关系(一对多关系)
  • 六、表关系(多对多关系)
  • 七、表联结(表联结where与join的用法)
  • 八、表联结(联结查询中的自联结)
  • 九、表联结(外部联结left join 与right join)
  • 十、UNION联合查询

一、子查询与表连接(子查询的使用)

⼦查询(嵌套sql)

SELECT语句是SQL的查询。迄今为⽌我们所看到的所有SELECT语句都是简单查询,即从单个数据库表中检索数据的单条语句。
SQL还允许创建⼦查询(subquery),即嵌套在其他查询中的查询。

利⽤⼦查询进⾏过滤

订单存储在两个表中。对于包含订单号、客户ID、订单⽇期的每个订单,orders表存储⼀⾏。 各订单的物品存储在相关的orderitems表中。orders表不存储客户信息。它只存储客户的ID。
实际的客户信息存储在customers表中。
现在,假如需要列出订购物品TNT2的所有客户,应该怎样检索?

复制此段代码到Navicat中运行得到如图所示的表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for customers
-- ----------------------------
DROP TABLE IF EXISTS `customers`;
CREATE TABLE `customers` (`cust_id` int(11) NOT NULL AUTO_INCREMENT,`cust_name` char(50) NOT NULL,`cust_address` char(50) DEFAULT NULL,`cust_city` char(50) DEFAULT NULL,`cust_state` char(5) DEFAULT NULL,`cust_zip` char(10) DEFAULT NULL,`cust_country` char(50) DEFAULT NULL,`cust_contact` char(50) DEFAULT NULL,`cust_email` char(255) DEFAULT NULL,PRIMARY KEY (`cust_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=10006 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;-- ----------------------------
-- Records of customers
-- ----------------------------
BEGIN;
INSERT INTO `customers` VALUES (10001, 'Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'Y Lee', 'ylee@coyote.com');
INSERT INTO `customers` VALUES (10002, 'Mouse House', '333 Fromage Lane', 'Columbus', 'OH', '43333', 'USA', 'Jerry Mouse', NULL);
INSERT INTO `customers` VALUES (10003, 'Wascals', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'rabbit@wascally.com');
INSERT INTO `customers` VALUES (10004, 'Yosemite Place', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Y Sam', 'sam@yosemite.com');
INSERT INTO `customers` VALUES (10005, 'E Fudd', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'E Fudd', NULL);
COMMIT;-- ----------------------------
-- Table structure for orderitems
-- ----------------------------
DROP TABLE IF EXISTS `orderitems`;
CREATE TABLE `orderitems` (`order_num` int(11) NOT NULL,`order_item` int(11) NOT NULL,`prod_id` char(10) NOT NULL,`quantity` int(11) NOT NULL,`item_price` decimal(8,2) NOT NULL,PRIMARY KEY (`order_num`,`order_item`) USING BTREE,KEY `fk_orderitems_products` (`prod_id`) USING BTREE,CONSTRAINT `fk_orderitems_orders` FOREIGN KEY (`order_num`) REFERENCES `orders` (`order_num`),CONSTRAINT `fk_orderitems_products` FOREIGN KEY (`prod_id`) REFERENCES `products` (`prod_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;-- ----------------------------
-- Records of orderitems
-- ----------------------------
BEGIN;
INSERT INTO `orderitems` VALUES (20005, 1, 'ANV01', 10, 5.99);
INSERT INTO `orderitems` VALUES (20005, 2, 'ANV02', 3, 9.99);
INSERT INTO `orderitems` VALUES (20005, 3, 'TNT2', 5, 10.00);
INSERT INTO `orderitems` VALUES (20005, 4, 'FB', 1, 10.00);
INSERT INTO `orderitems` VALUES (20006, 1, 'JP2000', 1, 55.00);
INSERT INTO `orderitems` VALUES (20007, 1, 'TNT2', 100, 10.00);
INSERT INTO `orderitems` VALUES (20008, 1, 'FC', 50, 2.50);
INSERT INTO `orderitems` VALUES (20009, 1, 'FB', 1, 10.00);
INSERT INTO `orderitems` VALUES (20009, 2, 'OL1', 1, 8.99);
INSERT INTO `orderitems` VALUES (20009, 3, 'SLING', 1, 4.49);
INSERT INTO `orderitems` VALUES (20009, 4, 'ANV03', 1, 14.99);
COMMIT;-- ----------------------------
-- Table structure for orders
-- ----------------------------
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (`order_num` int(11) NOT NULL AUTO_INCREMENT,`order_date` datetime DEFAULT NULL,`cust_id` int(11) NOT NULL,PRIMARY KEY (`order_num`) USING BTREE,KEY `fk_orders_customers` (`cust_id`) USING BTREE,CONSTRAINT `fk_orders_customers` FOREIGN KEY (`cust_id`) REFERENCES `customers` (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=20010 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;-- ----------------------------
-- Records of orders
-- ----------------------------
BEGIN;
INSERT INTO `orders` VALUES (20005, '2005-09-01 00:00:00', 10001);
INSERT INTO `orders` VALUES (20006, '2005-09-12 00:00:00', 10003);
INSERT INTO `orders` VALUES (20007, '2005-09-30 00:00:00', 10004);
INSERT INTO `orders` VALUES (20008, '2005-10-03 00:00:00', 10005);
INSERT INTO `orders` VALUES (20009, '2005-10-08 00:00:00', 10001);
COMMIT;-- ----------------------------
-- Table structure for productnotes
-- ----------------------------
DROP TABLE IF EXISTS `productnotes`;
CREATE TABLE `productnotes` (`note_id` int(11) NOT NULL AUTO_INCREMENT,`prod_id` char(10) NOT NULL,`note_date` datetime DEFAULT NULL,`note_text` text,PRIMARY KEY (`note_id`) USING BTREE,FULLTEXT KEY `note_text` (`note_text`)
) ENGINE=MyISAM AUTO_INCREMENT=115 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;-- ----------------------------
-- Records of productnotes
-- ----------------------------
BEGIN;
INSERT INTO `productnotes` VALUES (101, 'TNT2', '2005-08-17 00:00:00', 'Customer complaint:\r\nSticks not individually wrapped, too easy to mistakenly detonate all at once.\r\nRecommend individual wrapping.');
INSERT INTO `productnotes` VALUES (102, 'OL1', '2005-08-18 00:00:00', 'Can shipped full, refills not available.\r\nNeed to order new can if refill needed.');
INSERT INTO `productnotes` VALUES (103, 'SAFE', '2005-08-18 00:00:00', 'Safe is combination locked, combination not provided with safe.\r\nThis is rarely a problem as safes are typically blown up or dropped by customers.');
INSERT INTO `productnotes` VALUES (104, 'FC', '2005-08-19 00:00:00', 'Quantity varies, sold by the sack load.\r\nAll guaranteed to be bright and orange, and suitable for use as rabbit bait.');
INSERT INTO `productnotes` VALUES (105, 'TNT2', '2005-08-20 00:00:00', 'Included fuses are short and have been known to detonate too quickly for some customers.\r\nLonger fuses are available (item FU1) and should be recommended.');
INSERT INTO `productnotes` VALUES (106, 'TNT2', '2005-08-22 00:00:00', 'Matches not included, recommend purchase of matches or detonator (item DTNTR).');
INSERT INTO `productnotes` VALUES (107, 'SAFE', '2005-08-23 00:00:00', 'Please note that no returns will be accepted if safe opened using explosives.');
INSERT INTO `productnotes` VALUES (108, 'ANV01', '2005-08-25 00:00:00', 'Multiple customer returns, anvils failing to drop fast enough or falling backwards on purchaser. Recommend that customer considers using heavier anvils.');
INSERT INTO `productnotes` VALUES (109, 'ANV03', '2005-09-01 00:00:00', 'Item is extremely heavy. Designed for dropping, not recommended for use with slings, ropes, pulleys, or tightropes.');
INSERT INTO `productnotes` VALUES (110, 'FC', '2005-09-01 00:00:00', 'Customer complaint: rabbit has been able to detect trap, food apparently less effective now.');
INSERT INTO `productnotes` VALUES (111, 'SLING', '2005-09-02 00:00:00', 'Shipped unassembled, requires common tools (including oversized hammer).');
INSERT INTO `productnotes` VALUES (112, 'SAFE', '2005-09-02 00:00:00', 'Customer complaint:\r\nCircular hole in safe floor can apparently be easily cut with handsaw.');
INSERT INTO `productnotes` VALUES (113, 'ANV01', '2005-09-05 00:00:00', 'Customer complaint:\r\nNot heavy enough to generate flying stars around head of victim. If being purchased for dropping, recommend ANV02 or ANV03 instead.');
INSERT INTO `productnotes` VALUES (114, 'SAFE', '2005-09-07 00:00:00', 'Call from individual trapped in safe plummeting to the ground, suggests an escape hatch be added.\r\nComment forwarded to vendor.');
COMMIT;-- ----------------------------
-- Table structure for products
-- ----------------------------
DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (`prod_id` char(10) NOT NULL,`vend_id` int(11) NOT NULL,`prod_name` char(255) NOT NULL,`prod_price` decimal(8,2) NOT NULL,`prod_desc` text,PRIMARY KEY (`prod_id`) USING BTREE,KEY `fk_products_vendors` (`vend_id`) USING BTREE,CONSTRAINT `fk_products_vendors` FOREIGN KEY (`vend_id`) REFERENCES `vendors` (`vend_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;-- ----------------------------
-- Records of products
-- ----------------------------
BEGIN;
INSERT INTO `products` VALUES ('ANV01', 1001, '.5 ton anvil', 5.99, '.5 ton anvil, black, complete with handy hook');
INSERT INTO `products` VALUES ('ANV02', 1001, '1 ton anvil', 9.99, '1 ton anvil, black, complete with handy hook and carrying case');
INSERT INTO `products` VALUES ('ANV03', 1001, '2 ton anvil', 14.99, '2 ton anvil, black, complete with handy hook and carrying case');
INSERT INTO `products` VALUES ('DTNTR', 1003, 'Detonator', 13.00, 'Detonator (plunger powered), fuses not included');
INSERT INTO `products` VALUES ('FB', 1003, 'Bird seed', 10.00, 'Large bag (suitable for road runners)');
INSERT INTO `products` VALUES ('FC', 1003, 'Carrots', 2.50, 'Carrots (rabbit hunting season only)');
INSERT INTO `products` VALUES ('FU1', 1002, 'Fuses', 3.42, '1 dozen, extra long');
INSERT INTO `products` VALUES ('JP1000', 1005, 'JetPack 1000', 35.00, 'JetPack 1000, intended for single use');
INSERT INTO `products` VALUES ('JP2000', 1005, 'JetPack 2000', 55.00, 'JetPack 2000, multi-use');
INSERT INTO `products` VALUES ('OL1', 1002, 'Oil can', 8.99, 'Oil can, red');
INSERT INTO `products` VALUES ('SAFE', 1003, 'Safe', 50.00, 'Safe with combination lock');
INSERT INTO `products` VALUES ('SLING', 1003, 'Sling', 4.49, 'Sling, one size fits all');
INSERT INTO `products` VALUES ('TNT1', 1003, 'TNT (1 stick)', 2.50, 'TNT, red, single stick');
INSERT INTO `products` VALUES ('TNT2', 1003, 'TNT (5 sticks)', 10.00, 'TNT, red, pack of 10 sticks');
COMMIT;-- ----------------------------
-- Table structure for vendors
-- ----------------------------
DROP TABLE IF EXISTS `vendors`;
CREATE TABLE `vendors` (`vend_id` int(11) NOT NULL AUTO_INCREMENT,`vend_name` char(50) NOT NULL,`vend_address` char(50) DEFAULT NULL,`vend_city` char(50) DEFAULT NULL,`vend_state` char(5) DEFAULT NULL,`vend_zip` char(10) DEFAULT NULL,`vend_country` char(50) DEFAULT NULL,PRIMARY KEY (`vend_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1007 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;-- ----------------------------
-- Records of vendors
-- ----------------------------
BEGIN;
INSERT INTO `vendors` VALUES (1001, 'Anvils R Us', '123 Main Street', 'Southfield', 'MI', '48075', 'USA');
INSERT INTO `vendors` VALUES (1002, 'LT Supplies', '500 Park Street', 'Anytown', 'OH', '44333', 'USA');
INSERT INTO `vendors` VALUES (1003, 'ACME', '555 High Street', 'Los Angeles', 'CA', '90046', 'USA');
INSERT INTO `vendors` VALUES (1004, 'Furball Inc.', '1000 5th Avenue', 'New York', 'NY', '11111', 'USA');
INSERT INTO `vendors` VALUES (1005, 'Jet Set', '42 Galaxy Road', 'London', NULL, 'N16 6PS', 'England');
INSERT INTO `vendors` VALUES (1006, 'Jouets Et Ours', '1 Rue Amusement', 'Paris', NULL, '45678', 'France');
COMMIT;SET FOREIGN_KEY_CHECKS = 1;

(1) 查询包含物品TNT2的所有订单编号

# (1) 查询包含物品TNT2的所有订单编号
select order_num,prod_id from orderitems where prod_id = 'TNT2'


(2)查询对应订单号得到的用户的编号

# 查询对应订单号得到的用户的编号
select cust_id,order_num from orders where order_num in (20005,20007);


(3)查询购买对应物品的用户信息

#查询购买对应物品的用户信息
select cust_id,cust_name from customers where cust_id in(10001,10004);

另解:
可以把其中的WHERE⼦句转换为⼦查询⽽不是硬编码这些SQL返回的数据:

select cust_name,cust_contactfrom customerswhere cust_id in (select cust_idfrom orderswhere order_num IN (select order_numfrom orderitemswhere prod_id = 'TNT2'));--为了执⾏上述SELECT语句,MySQL实际上必须执⾏3条SELECT语句。
--最⾥边的⼦查询返回订单号列表,此列表⽤于其外⾯的⼦查询的WHERE⼦句。
--外⾯的⼦查询返回客户ID列表,此客户ID列表⽤于最外层查询的WHERE⼦句。
--最外层查询确实返回所需的数据。

二、子查询与表连接 (作为计算字段使用子查询)

假如需要显示customers表中每个客户的订单总数。订单与相应的客户ID存储在orders表中。
(1) 从customers表中检索客户列表。

select cust_id,cust_name from customers ;


(2) 对于检索出的每个客户,统计其在orders表中的订单数⽬。

select count(*) as orders from orders where cust_id = 10001;

为了对每个客户执⾏COUNT()计算,应该将COUNT()作为⼀个⼦查询。

select cust_id,cust_name,(select count(*)from orderswhere orders.cust_id = customers.cust_id) as orders_num
from customers
order by cust_name;

orders是⼀个计算字段,它是由圆括号中的⼦查询建⽴的。该⼦查询对检索出的每个客户执⾏⼀次。在此例⼦中,该⼦查询执⾏了5次,因为检索出了5个客户。

注意:⼦查询中的WHERE⼦句与前⾯使⽤的WHERE⼦句稍有不同,因为它使⽤了完全限定列名。
这种类型的⼦查询称为相关⼦查询。任何时候只要列名可能有多义性,就必须使⽤这种语法(表名和列名由⼀个句点分隔)。因为有两个cust_id列,⼀个在customers中,另⼀个在orders中,需要⽐较这两个列以正确地把订单与它们相应的顾客匹配。如果不完全限定列名,MySQL将假定你是对orders表中的cust_id进⾏⾃身⽐较。

三、表关系(什么是关系表)

SQL最强⼤的功能之⼀就是能在数据检索查询的执⾏中联结(join)表。
在能够有效地使⽤联结前,必须了解关系表以及关系数据库设计的⼀些基础知识。

--假如有⼀个包含产品⽬录的数据库表,其中每种类别的物品占⼀⾏。
--对于每种物品要存储的信息包括产品描述和价格,以及⽣产该产品的供应商信息。
产品表:
产品,描述,价格,供应商名称,供应商地址,供应商联系⽅式
A6 ... ... 奥迪 ...
520li .. .... 宝⻢ ... ...
...--现在,假如有由同⼀供应商⽣产的多种物品,那么在何处存储供应
--商信息(如,供应商名、地址、联系⽅法等)呢?
产品,描述,价格,供应商名称,供应商地址,供应商联系⽅式
A6 ... ... 奥迪 ... ....
520li .. .... 宝⻢ ... ...
A8 .. ... 奥迪 ... ...

相同数据出现多次绝不是⼀件好事,此因素是关系数据库设计的基础。
关系表的设计就是要保证把信息分解成多个表,⼀类数据⼀个表。
各表通过某些常⽤的值(即关系设计中的关系(relational))互相关联。
在这个例⼦中,可建⽴两个表,⼀个存储供应商信息,另⼀个存储产品信息。

-- vendors表包含所有供应商信息
|vend_id | vend_name | vend_address| vend_city ....-- products表只存储产品信息,它除了存储供应商ID(vendors表的主键)外不存储其他供应商信息。
prod_id | vend_id | prod_name | prod_price | prod_desc

vendors表的主键⼜叫作products的外键,它将vendors表与products表关联,利⽤供应商ID能 从vendors表中找出相应供应商的详细信息。 这样做的好处如下:

  • 供应商信息不重复,从⽽不浪费时间和空间;
  • 如果供应商信息变动,可以只更新vendors表中的单个记录,相关表中的数据不⽤改动;
  • 由于数据⽆重复,显然数据是⼀致的,这使得处理数据更简单

关系数据可以有效地存储和⽅便地处理。因此,关系数据库的可伸缩性远⽐⾮关系数据库要好。

四、表关系(一对一关系与外键)

外键:在一个表中,定义一个字段,这个字段中存储的数据是另外一张表中的主键,就是在一个表中的字段,代表着这个数据属于谁。
了解:外键实现的方式,有两种:物理外键、逻辑外键

  • 物理外键:就是在创建表时,就指定这个表中的字段是一个外键,并且强关联某个表中的某个字段,需要在定义字段时,使用sql语句来实现。
  • 逻辑外键:就是在表中创建一个普通的字段,没有强关联关系,需要通过程序逻辑来实现。

一对一: 就是在一个表中的数据,对应着另外一张表中的一个数据,只能有一个。

上面的表关系就是一对一的表关系,通过详情表中的yid这个字段来标记员工表中的主键。
一个员工有着一个对应的详情信息,存储在详情表中,
在详情表中的数据,也只属于某一个员工。

五、表关系(一对多关系)

**一对多:**在一个表中的一条数据对应着另外一个表中的多条数据,在一个表中的多条数据,对应着另外一张表中一个数据。

mysql> select * from orders;

mysql> select * from orderitems;

六、表关系(多对多关系)

多对多:
举例:例如一本书,有多个标签,同时每一个标签下又对应多本书。

案例二:
一个班级有多个老师来讲课(化学、物理、数学、、、)
一个老师要带多个班级 (一班,二班,三班)

七、表联结(表联结where与join的用法)

**表联结:**就是一种查询的机制,用来在一个select语句中关联多个表进行查询,称为联结。

需要查询出所有商品以及对应的供应商信息?
供应商名称,商品名称,商品价格

select vend_name,prod_name,prod_price
from vendors,products
where vendors.vend_id  = products.vend_id
order by vend_name

假如没有where条件时会发生什么呢?

select vend_name,prod_name,prod_price
from vendors,products;

如果没有where条件,那么第一个表中的每一行数据会与第二个表中的每一行数据进行匹配,不管逻辑是否可以匹配。
如果没有where条件,那么这种结果称为 笛卡尔积,第一个表的行数乘以第二个表中的行数。

所以千万不要忘记where条件!!!

除了使用where进行表的联结查询外,还可以使用另外一种联结方式,join

select vend_name,prod_name,prod_price
from vendors
inner join products on vendors.vend_id = products.vend_id;

上面这个sql就是使用了 join 的语法,进行了两个表的联结,在 on 后面 去定义了 联结的条件。

联结多个表

案例: 查询出订单号为20005的订单中购买的商品及对应的产品供应商信息

select prod_name,vend_name,prod_price,quantity
from orderitems,products,vendorswhere products.vend_id = vendors.vend_idand orderitems.prod_id = products.prod_idand order_num = 20005;

改写为 join 的语法:

select prod_name,vend_name,prod_price,quantity
from orderitems
inner join products on orderitems.prod_id = products.prod_id
inner join vendors on products.vend_id = vendors.vend_id
where order_num = 20005;

八、表联结(联结查询中的自联结)

自联结:当前这个表与自己这个表做联结(join)

假如你发现某物品(其ID为DTNTR)存在问题,因此想知道生产该物品的供应商生产的其他物品是否也存在这些问题。
此查询要求首先找到生产ID为DTNTR的物品的供应商,然后找出这个供应商生产的其他物品。

-- 使用子查询(嵌套查询)
select prod_id,prod_name
from products
where vend_id = (select vend_id from products where prod_id = 'DTNTR');

-- 使用 自联结方式查询
select p1.prod_id,p2.prod_name
from products as p1
join products as p2 on p1.vend_id = p2.vend_id
where p2.prod_id = 'DTNTR';
-- 改成where语句
select p1.prod_id,p2.prod_name
from products as p1, products as p2
where p1.vend_id = p2.vend_id and p2.prod_id = 'DTNTR';

-- 深入了解 join
select
p1.prod_id,p1.prod_name,p1.vend_id,
p2.prod_id,p2.prod_name,p2.vend_id
from products as p1,products as p2
where p1.vend_id = p2.vend_id
and p2.prod_id = 'DTNTR';

子查询(嵌套查询) 是目前可明确知道的 sql中运行效率最低的一种方式,尽可能不使用嵌套语句。

九、表联结(外部联结left join 与right join)

使用 where 进行关联查询 (内部联结) 只能对两个表中相关联的数据进行查询

select customers.cust_id,orders.order_num
from customers,orders
where orders.cust_id = customers.cust_id;

或者

select customers.cust_id,orders.order_num
from customers join orders
on orders.cust_id = customers.cust_id;

# 查看所有用户
select  cust_id from customers;


那什么是外部联结呢?
left join : 是以 left join 左侧表为基准,去关联右侧的表进行联结,如果有未关联的数据,那么结果为null。
right join:是以 right join 右侧表为基准,去关联左侧的表进行联结,如果有未关联的数据,那么结果为null。

以用户表为基准,去关联查询 订单表数据

select customers.cust_id,orders.order_num
from customers left join orders
on customers.cust_id = orders.cust_id;
select customers.cust_id,orders.order_num
from orders right join customers
on customers.cust_id = orders.cust_id;

对每个客户下了多少订单进行计数,包括那些至今尚未下订单的客户;

select customers.cust_id,count(orders.order_num) as nums
from customers left join orders
on customers.cust_id = orders.cust_id
group by customers.cust_id;

十、UNION联合查询

MySQL也允许执⾏多个查询(多条SELECT语句),并将结果作为单个查询结果集返回。
这些组合查询通常称为并(union)或复合查询(compound query)。

UNION规则

  • UNION必须由两条或两条以上的SELECT语句组成,语句之间⽤关键字UNION分隔(因此,如果组合4条SELECT语句,将要使⽤3个UNION关键字)。
  • UNION中的每个查询必须包含相同的列、表达式或聚集函数(不过各个列不需要以相同的次序列出)
  • 列数据类型必须兼容:类型不必完全相同,但必须是DBMS可以隐含地转换的类型(例如,不同的数值类型或不同的⽇期类型)。

假如需要价格⼩于等于5的所有物品的⼀个列表,⽽且还想包括供应商1001和1002⽣产的所有物品。

-- 先查询第⼀个结果
select vend_id,prod_id,prod_price
from products
where prod_price <= 5;

-- 再查询第⼆个结果
select vend_id,prod_id,prod_price
from products
where vend_id in(1001,1002);

--使⽤union将两个sql⼀并执⾏
select vend_id,prod_id,prod_price from products where prod_price <= 5
union
select vend_id,prod_id,prod_price from products where vend_id in(1001,1002);
  • 这条语句由前⾯的两条SELECT语句组成,语句中⽤UNION关键字分隔。
  • UNION指示MySQL执⾏两条SELECT语句,并把输出组合成单个查询结果集。
  • 以下是同样结果,使⽤where的多条件来实现。
select vend_id,prod_id,prod_price
from products
where prod_price <= 5 or vend_id in (1001,1002);

在这个简单的例⼦中,使⽤UNION可能⽐使⽤WHERE⼦句更为复杂。
但对于更复杂的过滤条件,或者从多个表(⽽不是单个表)中检索数据的情形,使⽤UNION可能会使处理更简单。

UNION从查询结果集中⾃动去除了重复的⾏(换句话说,它的⾏为与单条SELECT语句中使⽤多个WHERE⼦句条件⼀样)。

这是UNION的默认⾏为,但是如果需要,可以改变它。如果想返回所有匹配⾏,可使⽤UNION ALL⽽不是UNION。

  • 对组合查询结果排序

SELECT语句的输出⽤ORDER BY⼦句排序。在⽤UNION组合查询时,只能使⽤⼀条ORDER BY⼦句,它必须出现在最后⼀条SELECT语句之后。

对于结果集,不存在⽤⼀种⽅式排序⼀部分,⽽⼜⽤另⼀种⽅式排序另⼀部分的情况,因此不允许使⽤多条ORDER BY⼦句。

select vend_id,prod_id,prod_price from products where prod_price <= 5
union
select vend_id,prod_id,prod_price from products where vend_id in(1001,1002)
order by prod_price;

  • 这条UNION在最后⼀条SELECT语句后使⽤了ORDER BY⼦句。
  • 虽然ORDER BY⼦句似乎只是最后⼀条SELECT语句的组成部分,但实际上MySQL将⽤它来排序所有SELECT语句返回的所有结果。

第六章 (数据库) 第3节 数据库高级相关推荐

  1. 软考-架构师-第三章-数据库系统 第七节 数据库设计(读书笔记)

    版权声明 主要针对希赛出版的架构师考试教程<系统架构设计师教程(第4版)>,作者"希赛教育软考学院".完成相关的读书笔记以便后期自查,仅供个人学习使用,不得用于任何商业 ...

  2. 物流信息管理系统MySQL设计_案例分析第六章:物流管理系统的数据库设计(六个基本步骤)案例分析...

    物流管理信息系统的数据库设计 (案例分析) 系统名称:物流管理信息系统(数据库管理系统) 一.需求分析 需求分析是整个数据库设计过程的基础,要收集数据库所有用户的信息内容和处理要求,并加以规格化和分析 ...

  3. 第六章 程序数据集散地:数据库

    1.上机练习 1.上机练习1  (新建数据库) 2.选择题 1.C          2.D          3.A          4.B          5.D          6.A  ...

  4. 第一行代码 第六章 数据储存方案 - LitePal数据库存储

    LitePal – 一款开源的Android数据库框架. 它采用对象关系映射的模式(ORM),将开发中常用的数据库功能进行封装,使得不用编写SQL语句就可以完成各种建表和增删改查的操作. 在项目中使用 ...

  5. (王道408考研数据结构)第六章图-第四节4:最短路径之迪杰斯特拉算法(思想、代码、演示、答题规范)

    文章目录 一:BFS算法局限性 二:迪杰斯特拉(dijkstra)算法基本思想 三:迪杰斯特拉(dijkstra)算法代码实现 四:迪杰斯特拉(dijkstra)算法代码视频演示 五:迪杰斯特拉(di ...

  6. (王道408考研数据结构)第六章图-第四节5:最短路径之弗洛伊德算法(思想、代码、演示、答题规范)

    文章目录 一:动态规划基本思想 二:弗洛伊德(Floyd)算法基本思想 三:弗洛伊德(Floyd)算法代码实现 四:弗洛伊德(Floyd)算法代码视频演示 五:弗洛伊德(Floyd)算法代码答题规范 ...

  7. (王道408考研数据结构)第六章图-第四节3:最短路径之BFS算法(思想、代码、演示、答题规范)

    文章目录 一:BFS算法基本思想 二:BFS算法代码 三:反思 最短路径shortestpath):主要有以下两类最短路径问题 单源最短路径问题:一个顶点到其他顶点最短路径 迪杰斯特拉算法(dijks ...

  8. (王道408考研数据结构)第六章图-第四节1:最小生成树之普利姆算法(思想、代码、演示、答题规范)

    文章目录 一:普利姆(Prim)算法算法思想 二:普利姆(Prim)算法注意点 三:普利姆(Prim)算法代码实现 四:普利姆(Prim)算法代码视频演示 五:普利姆(Prim)算法动画演示 六:普利 ...

  9. (王道408考研数据结构)第六章图-第四节7:关键路径(最早发生时间、最迟发生时间)

    文章目录 一:关键路径基本概念 (1)AOE网 (2)AOV网和AOE网的对比 (3)关键路径 二:手动求解关键路径 (1)每个事件(即顶点)的最早发生时间和最迟发生时间 (2)每个活动(即边)的最早 ...

  10. (王道408考研数据结构)第六章图-第四节6:拓扑排序(AOV网、代码、排序规则)

    文章目录 一:拓扑排序基本概念 (1)AOV网 (2)拓扑序列 二:拓扑排序 (1)拓扑排序 (2)拓扑排序规则 三:拓扑排序代码实现 (1)准备工作 (2)代码 (3)代码分析 一:拓扑排序基本概念 ...

最新文章

  1. 密度聚类 DBSCAN python实现
  2. 【SQL编程】MySQL 5.7.28 版本使用 SQL 直接解析 JSON 字符串(判断是否是合法JSON类型+文本深度+文本长度+值类型+keys获取+值获取+不同深度数据获取)
  3. Linux 关闭桌面方法
  4. python pip工具命令_python 工具链 包管理工具 pip
  5. IHttpModule的那些事
  6. SEO市场是在扩大还是缩小 Seoer终于会变成什么?
  7. 软考初级程序员含金量高吗?有答案了
  8. framework层Service的基础知识和常见问题
  9. 【数学建模】预测模型之BP网络预测
  10. 程序员年薪40万被国企同学怒怼:没啥贡献,凭什么工资这么高!
  11. ffmpeg常用操作 - 录屏 - 转码
  12. FTP、TFTP 实现 NAT ALG 访问
  13. 腾讯互娱AMS | 我的打包我做主——浅析前端构建
  14. 软件工程导论—软件与软件工程
  15. 获取windows系统ip、计算机名、当前用户名
  16. 英孚教育APP应用获全球杰出商业银奖
  17. STM32 FLASH的擦写寿命
  18. 【xinfanqie】解决网络出现“正在连接”的问题
  19. Java实现台阶问题
  20. 通用SQL查询分析器

热门文章

  1. Kubernetes 污点介绍
  2. AtCoder Grand Contest 058 B Adjacent Chmax
  3. 麦吉尔 计算机科学学分,麦吉尔大学计算机
  4. 谷歌2018年IO大会
  5. 互联网广告行业术语及缩写
  6. 使用 Kotlin 中的 takeIf
  7. 知我者,谓我心忧,不知我者,谓我何求。何必说?
  8. Java查询Mysql数据库时区问题(相差13/14)个小时
  9. F - Endless Walk
  10. 【线性代数(6)】范德蒙德行列式及克莱姆法则