本文实例总结了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');
更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》及《MySQL数据库锁相关技巧汇总》
希望本文所述对大家MySQL数据库计有所帮助。
MySQL,子查询
更新动态
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]