MySQL数据库操作中常用的一些SQL语句

本文介绍了多种实用的SQL查询技巧,包括单句SQL查询、多条件筛选、子查询转换、联合查询、使用IN与EXISTS等,并通过实例对比了不同方法的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.单句SQL语句,从视图中查询结果
select c.product_no, c.product_flag from
( select a.product_flag,b.product_no from product_category as a join product as b on (a.product_category_no=b.product_type)) as c
where c.product_no=8
 
2.根据“年费”查询多个字段
select product_no,product_property_no,product_property_detail_content     from product_property_detail    
     where product_property_name="年费" and (product_property_detail_content between 360 and 500)
 
3.子查询转化“编号”为“名字”
select product_no,product_name,creditcard_rating,
( select bank.bank_name from bank where bank.bank_no=product.company_no) as company_no,product_desc
     from product where product_type=12 and company_no= 2    
 
4.联合查询得详细信息
SELECT
( select product_property_detail_content from product_property_detail where product_no=8 and product_property_no=30) as dhsx,
product_function as jbgn,
(( select c.product_flag from ( select a.product_flag,b.product_no from product_category as a join product as b on (a.product_category_no=b.product_type)) as c where c.product_no=8 ) ) as cplb    
FROM    product where product_no=8
 
5.union操作
select product_no     from product_property_detail    
where product_property_no=3 and (product_property_detail_content between 360 and 500)    
union all
select product_no from product where product_type=12 and company_no= 2    
order by product_no
 
6.多条件查询
select ( select product_property_name from product_property as a where a.product_property_no=product_property_detail.product_property_no)
as name,product_property_detail_content as jbgn
from product_property_detail where product_no=8 and    
(product_property_no=1 or product_property_no=13 or product_property_no=14 or product_property_no=15 )
 
7.in语句
select product_no from product where product_no    
in    
( select product_no from product where creditcard_rating="金卡")
 
8.求合集
select * from ( select product_no     from product_property_detail    
     where product_property_no=3 and (product_property_detail_content between 360 and 500) ) as a
inner join
( select product_no from product where product_type=12 and company_no= 2 ) as b
on    a.product_no=b.product_no    
 
9.Exists better than In
 背景介绍:product_property_detail表中 product_property_no为种类, product_no为商品标号,他们为多对多关系,主键为: product_property_detail_no,该表只有种类和商品号确定才能找到该唯一的记录号!
测试:找每种种类中商品标号最大的那个商品
方案一:In方式
select product_property_no,product_no,product_name from product_property_detail a
where product_no in (
select max(product_no) from product_property_detail    
where product_property_no = a.product_property_no    
)
结果:
42 rows in set (40.10 sec)
 
方案二:Exists方式
select product_property_no,product_no,product_name from product_property_detail a
where not exists (
select 1 from product_property_detail    
where product_property_no = a.product_property_no and    
product_no > a.product_no
)
结果:
42 rows in set (14.69 sec)
小结:
在查找复杂关系的数据时候Exists会有更好的效率!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值