一聚教程网:一个值得你收藏的教程网站

最新下载

欧洲75秒秒速赛车漏洞:MySQL子查询操作实例详解

时间:2018-07-04 09:39:14 编辑:猪哥 来源:转载

本文实例总结了MySQL子查询操作。分享给大家供大家参考,具体如下:

定义两个表tb1和tb2

CREATE table tbl1 ( num1 INT NOT NULL);
CREATE table tbl2 ( num2 INT NOT NULL);

向两个表中插入数据:

INSERT INTO tbl1 values(1), (5), (13), (27);
INSERT INTO tbl2 values(6), (14), (11), (20);

any some关键字的子查询

SELECT num1
FROM tbl1
WHERE num1 > ANY (SELECT num2 FROM tbl2);

all关键字的子查询

SELECT num1
FROM tbl1
WHERE num1 > ALL (SELECT num2 FROM tbl2);

exists关键字的子查询

SELECT * from fruits
WHERE EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);

SELECT * from fruits
WHERE f_price>10.20 AND EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);

SELECT * from fruits
WHERE NOT EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);

in关键字的子查询

SELECT c_id
FROM orders
WHERE o_num IN (SELECT o_num FROM orderitems WHERE f_id = 'c0');

SELECT c_id
FROM orders
WHERE o_num NOT IN (SELECT o_num FROM orderitems WHERE f_id = 'c0');

带比较运算符的子查询

SELECT s_id, f_name FROM fruits
WHERE s_id =
(SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');

<>所有非

SELECT s_id, f_name FROM fruits
WHERE s_id <>
(SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');

定义两个表tb1和tb2

CREATE table tbl1 ( num1 INT NOT NULL);
CREATE table tbl2 ( num2 INT NOT NULL);

向两个表中插入数据

INSERT INTO tbl1 values(1), (5), (13), (27);
INSERT INTO tbl2 values(6), (14), (11), (20);

【例.53】返回tbl2表的所有 num2 列,然后将 tbl1 中的 num1 的值与之进行比较,只要大于 num2的任何值为符合查询条件的结果

SELECT num1
FROM tbl1
WHERE num1 > ANY (SELECT num2 FROM tbl2);

【例.54】返回tbl1表的中比tbl2表num2 列所有值都大的值

SELECT num1
FROM tbl1
WHERE num1 > ALL (SELECT num2 FROM tbl2);

【例.55】查询表suppliers表中是否存在s_id=107的供应商,如果存在则查询fruits表中的记录

SELECT * from fruits
WHERE EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);

【例.56】查询表suppliers表中是否存在s_id=107的供应商,如果存在则查询fruits表中的f_price大于10.20的记录

SELECT * from fruits
WHERE f_price>10.20 AND EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);

【例.57】查询表suppliers表中是否存在s_id=107的供应商,如果不存在则查询fruits表中的记录

SELECT * from fruits
WHERE NOT EXISTS
(SELECT s_name FROM suppliers WHERE s_id = 107);

【例.58】在orderitems表中查询订购f_id为c0的订单号,并根据订单号查询具有订单号的客户c_id

SELECT c_id FROM orders WHERE o_num IN
(SELECT o_num FROM orderitems WHERE f_id = 'c0');

【例.59】与前一个例子语句类似,但是在SELECT语句中使用NOT IN操作符

SELECT c_id FROM orders WHERE o_num NOT IN
(SELECT o_num FROM orderitems WHERE f_id = 'c0');

【例.60】在suppliers表中查询s_city等于Tianjin的供应商s_id,然后在fruits表中查询所有该供应商提供的水果的种类

SELECT s_id, f_name FROM fruits
WHERE s_id =
(SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');

【例.61】在suppliers表中查询s_city等于Tianjin的供应商s_id,然后在fruits表中查询所有非该供应商提供的水果的种类,SQL语句如下:

SELECT s_id, f_name FROM fruits
WHERE s_id <>
(SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');

文章评论

热门栏目

  • 荆门全面实现社会保障卡即时制卡 2019-05-20
  • 【春到渭南】随手拍渭南各地区春景,一贴看尽渭南春色! 2019-05-20
  • 汪峰章子怡带女儿醒醒看马戏表演 满脸幸福 2019-05-19
  • 爱护民生:什么基金都不能买,即使获利,也不会给分多少红利,只是意思意思。 2019-05-19
  • 北京市北京盈之宝4S店【在线咨询】 2019-05-18
  • 美媒:研究发现高血压会增加痴呆风险 2019-05-18
  • 房价还会跌吗?这几条黄金线索告诉你背后的事实 ——凤凰网房产西安 2019-05-17
  • 搞市场经济,也不意味这政府就能偷懒,不是什么都甩给市场,美其名曰"市场会自我调节" 2019-05-17
  • 经常吃它到老耳聪目明牙齿好 让你越来越健康-美食资讯 2019-05-16
  • 端午小长假杭州接待游客454.8万人次 9成民宿预订一空 2019-05-16
  • 十九大党章修正案的新时代意蕴 2019-05-15
  • 勇敢挑战“不可能”(今日谈) 2019-05-14
  • 第十三届“三晋之春”合唱比赛在阳泉开幕 2019-05-14
  • 驴友在北京凤凰岭失联 百人搜救七天仍未找到踪迹 2019-05-13
  • 光明网招聘时事新闻分析编辑 2019-05-12
  • 694| 125| 682| 514| 636| 968| 318| 100| 726| 196|