时间:2021-05-24
本文实例总结了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 num1FROM tbl1WHERE num1 > ANY (SELECT num2 FROM tbl2);all关键字的子查询
SELECT num1FROM tbl1WHERE num1 > ALL (SELECT num2 FROM tbl2);exists关键字的子查询
SELECT * from fruitsWHERE EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);SELECT * from fruitsWHERE f_price>10.20 AND EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);SELECT * from fruitsWHERE NOT EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);带in关键字的子查询
SELECT c_idFROM ordersWHERE o_num IN (SELECT o_num FROM orderitems WHERE f_id = 'c0');SELECT c_idFROM ordersWHERE o_num NOT IN (SELECT o_num FROM orderitems WHERE f_id = 'c0');带比较运算符的子查询
SELECT s_id, f_name FROM fruitsWHERE s_id =(SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');<>所有非
SELECT s_id, f_name FROM fruitsWHERE 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 num1FROM tbl1WHERE num1 > ANY (SELECT num2 FROM tbl2);【例.54】返回tbl1表的中比tbl2表num2 列所有值都大的值
SELECT num1FROM tbl1WHERE num1 > ALL (SELECT num2 FROM tbl2);【例.55】查询表suppliers表中是否存在s_id=107的供应商,如果存在则查询fruits表中的记录
SELECT * from fruitsWHERE EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);【例.56】查询表suppliers表中是否存在s_id=107的供应商,如果存在则查询fruits表中的f_price大于10.20的记录
SELECT * from fruitsWHERE f_price>10.20 AND EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);【例.57】查询表suppliers表中是否存在s_id=107的供应商,如果不存在则查询fruits表中的记录
SELECT * from fruitsWHERE 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 fruitsWHERE 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 fruitsWHERE s_id <>(SELECT s1.s_id from suppliers AS s1 WHERE s1.s_city = 'Tianjin');更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》及《MySQL数据库锁相关技巧汇总》
希望本文所述对大家MySQL数据库计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了mysql连接查询、联合查询、子查询原理与用法。分享给大家供大家参考,具体如下:本文内容:连接查询联合查询子查询from子查询where子查询ex
MySQL表子查询表子查询是指子查询返回的结果集是N行N列的一个表数据。MySQL表子查询实例下面是用于例子的两张原始数据表:article表:blog表:SQ
本文实例讲述了oracle基本查询操作子查询用法。分享给大家供大家参考,具体如下:一、子查询语法SELECTselect_listFROMtableWHEREe
本文实例讲述了mysql关联子查询的一种优化方法。分享给大家供大家参考,具体如下:很多时候,在mysql上实现的子查询的性能较差,这听起来实在有点难过。特别有时
MySQL设计和命令行模式下建立详解系列文章:MySQL设计和命令行模式下建立详解C++利用MySQLAPI连接和操作数据库实例详解1.数据表的设计MySQL数