SQL106. 将两个 SELECT 语句结合起来(一)
描述
prod_id | quantity |
a0001 | 105 |
a0002 | 100 |
a0002 | 200 |
a0013 | 1121 |
a0003 | 10 |
a0003 | 19 |
a0003 | 5 |
BNBG | 10002 |
prod_id | quantity |
a0002 | 100 |
BNBG | 10002 |
示例1
输入:
DROP TABLE IF EXISTS `OrderItems`; CREATE TABLE IF NOT EXISTS `OrderItems`( prod_id VARCHAR(255) NOT NULL COMMENT '产品id', quantity VARCHAR(255) NOT NULL COMMENT '商品数量' ); INSERT `OrderItems` VALUES ('a0001',105),('a0002',100),('a0002',200),('a0013',1121),('a0003',10),('a0003',19),('a0003',5),('BNBG',10002);
输出:
a0002|100.000 BNBG|10002.000
Mysql 解法, 执行用时: 39ms, 内存消耗: 6440KB, 提交时间: 2022-03-06
select prod_id , quantity from OrderItems where quantity=100 union select prod_id , quantity from OrderItems where prod_id like 'BNBG%';
Mysql 解法, 执行用时: 39ms, 内存消耗: 6548KB, 提交时间: 2022-03-07
select * from OrderItems where quantity=100 union all select * from OrderItems where prod_id like 'BNBG%' order by prod_id;
Mysql 解法, 执行用时: 39ms, 内存消耗: 6564KB, 提交时间: 2022-03-03
select prod_id, quantity from OrderItems where Prod_id like'BNBG%' OR quantity=100
Mysql 解法, 执行用时: 39ms, 内存消耗: 6572KB, 提交时间: 2022-03-07
select prod_id, quantity from OrderItems where quantity = 100 union select prod_id, quantity from OrderItems where prod_id like 'BNBG%'
Mysql 解法, 执行用时: 39ms, 内存消耗: 6580KB, 提交时间: 2022-03-05
select prod_id, quantity from OrderItems # where Prod_id like'BNBG%' OR quantity=100 where Prod_id rlike'^BNBG' OR quantity=100