欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL子查询操作实例详解

程序员文章站 2022-03-29 18:41:06
本文实例总结了mysql子查询操作。分享给大家供大家参考,具体如下: 定义两个表tb1和tb2 create table tbl1 ( num1 int not...

本文实例总结了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数据库计有所帮助。