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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS PostgreSQL,高可用系列 2核4GB
简介:
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会有更好的效率!




    本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/105616,如需转载请自行联系原作者

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
2月前
|
存储 SQL 关系型数据库
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
|
4月前
|
SQL 存储 关系型数据库
第二篇:关系型数据库的核心概念与 SQL 基础
本篇内容深入浅出地讲解了关系型数据库的核心概念与SQL基础,适合有一定计算机基础的学习者。文章涵盖数据库的基本操作(CRUD)、数据类型、表的创建与管理等内容,并通过实例解析SELECT、INSERT、UPDATE、DELETE等语句的用法。此外,还推荐了多种学习资源与实践建议,帮助读者巩固知识。学完后,你将掌握基础数据库操作,为后续高级学习铺平道路。
210 1
|
3月前
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
234 62
|
2月前
|
SQL XML Java
配置Spring框架以连接SQL Server数据库
最后,需要集成Spring配置到应用中,这通常在 `main`方法或者Spring Boot的应用配置类中通过加载XML配置或使用注解来实现。
218 0
|
4月前
|
SQL 数据采集 关系型数据库
实现MySQL与SQL Server之间数据迁移的有效方法
总的来说,从MySQL到SQL Server的数据迁移是一个涉及到很多步骤的过程,可能会遇到各种问题和挑战。但只要精心规划、仔细执行,这个任务是完全可以完成的。
280 18
|
3月前
|
SQL 人工智能 关系型数据库
GitHub 热门!MindsDB 破解 AI + 数据库瓶颈,究竟有什么惊艳亮点?只需 SQL 即可实现智能预测
MindsDB 是一款将 AI 能力直接注入数据库的开源工具,支持 MySQL、PostgreSQL 等多种数据库连接,通过 SQL 即可完成模型训练与预测。它提供 AutoML 引擎、LLM 集成、联邦查询等功能,简化 MLOps 流程,实现数据到智能的无缝衔接。项目在 GitHub 上已获 32.4k 星,社区活跃,适用于客户流失预警、推荐系统、情感分析等场景。开发者无需深入模型细节,即可快速构建智能解决方案。项目地址:https://github.com/mindsdb/mindsdb。
688 0
|
5月前
|
SQL 关系型数据库 MySQL
【MySQL】SQL分析的几种方法
以上就是SQL分析的几种方法。需要注意的是,这些方法并不是孤立的,而是相互关联的。在实际的SQL分析中,我们通常需要结合使用这些方法,才能找出最佳的优化策略。同时,SQL分析也需要对数据库管理系统,数据,业务需求有深入的理解,这需要时间和经验的积累。
152 12
|
12月前
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
407 13
解锁 SQL Server 2022的时间序列数据功能
【7月更文挑战第14天】要解锁SQL Server 2022的时间序列数据功能,可使用`generate_series`函数生成整数序列,例如:`SELECT value FROM generate_series(1, 10)。此外,`date_bucket`函数能按指定间隔(如周)对日期时间值分组,这些工具结合窗口函数和其他时间日期函数,能高效处理和分析时间序列数据。更多信息请参考官方文档和技术资料。
261 9

热门文章

最新文章

推荐镜像

更多