复杂sql 查询编写方法_学习SQL:如何编写复杂的SELECT查询

复杂sql 查询编写方法

In my career, I’ve heard many times, things like “How to write a complex SELECT query?”, “Where to start from?” or “This query looks so complex. How you’ve learned to write such complex queries?”. While I would like to think of myself as of a brilliant mind or genius or add something like “query magician” to my social network profiles, well, writing complex SQL wouldn’t be the only thing required to do that. Therefore, in this article, I’ll try to cover the “magic” behind writing complex SELECT statements.

在我的职业生涯中,我听到过很多次,例如“如何编写一个复杂的SELECT查询?”,“从哪里开始?”。 或“此查询看起来如此复杂。 您如何学习编写这样的复杂查询?”。 尽管我想将自己想象成一个聪明的头脑或天才,或者在我的社交网络配置文件中添加诸如“查询魔术师”之类的东西,但是,编写复杂SQL并不是唯一要做的事情。 因此,在本文中,我将尝试介绍编写复杂的SELECT语句背后的“魔术”。

该模型 (The Model)

As always, I’ll start with the data model we’ll be using. Before you start to write (complex) queries you should understand what is where – which tables stored what data. Also, you should understand the nature of relations between these tables.

与往常一样,我将从我们将要使用的数据模型开始。 在开始编写(复杂)查询之前,您应该了解什么在哪里–哪些表存储了哪些数据。 另外,您应该了解这些表之间关系的性质。

How to Write a Complex SELECT Query - the data model we'll use

If you don’t have these two on disposal, you have 3 options:

如果您没有这两个选项,则有3种选择:

  • Ask somebody who created the model for the documentation (if that person is available). Same stands for understanding the business logic behind the data

    询问为文档创建模型的人员(如果该人员可用)。 同样代表理解数据背后的业务逻辑
  • Create documentation yourself. This takes time, but is really very useful, especially if you jump in the middle of an undocumented project

    自己创建文档。 这需要时间,但确实非常有用,尤其是当您跳入未记录项目的中间时
  • You can always do it without the documentation, but you should be pretty sure you know what you’re doing. E.g. I wouldn’t recommend you driving a car where I’ve repaired brakes. I mean, you can try it, but…

    您始终可以在没有文档的情况下进行操作,但是您应该确定自己知道自己在做什么。 例如,我不建议您驾驶修理了刹车的汽车。 我的意思是,您可以尝试一下,但是…

All these tips can be used regardless of what you are doing with your database. Having the overall picture will spare you a lot of time in the long-run, so invest some time when you’re starting.

无论您对数据库做什么,都可以使用所有这些技巧。 从长远来看,拥有整体情况会节省大量时间,因此在开始时要花一些时间。

让我们从复杂查询开始 (Let’s Start with the Complex Query)

In case I spent too many words so far, let’s remind ourselves of the original question – “How to write a complex SELECT query?”. And let’s start with a complex query.

如果到目前为止我花了太多的单词,让我们想起最初的问题–“如何编写一个复杂的SELECT查询?”。 让我们从一个复杂的查询开始。

SELECT 
	country.country_name_eng,
	SUM(CASE WHEN call.id IS NOT NULL THEN 1 ELSE 0 END) AS calls,
	AVG(ISNULL(DATEDIFF(SECOND, call.start_time, call.end_time),0)) AS avg_difference
FROM country 
LEFT JOIN city ON city.country_id = country.id
LEFT JOIN customer ON city.id = customer.city_id
LEFT JOIN call ON call.customer_id = customer.id
GROUP BY 
	country.id,
	country.country_name_eng
HAVING AVG(ISNULL(DATEDIFF(SECOND, call.start_time, call.end_time),0)) > (SELECT AVG(DATEDIFF(SECOND, call.start_time, call.end_time)) FROM call)
ORDER BY calls DESC, country.id ASC;

And this is what query returns:

这是查询返回的内容:

complex SQL SELECT query

As you can see, we have a complex query and 2 rows in the result. Without any comments, we can’t easily say what does this query does and how it works. Let’s change that now.

如您所见,我们有一个复杂的查询,结果中有2行。 没有任何评论,我们不能轻易说出此查询的功能及其工作方式。 让我们现在改变它。

如何编写复杂的SELECT查询和数据在哪里? (How to Write a Complex SELECT Query & Where is the Data?)

We’re back to the original question. Now, we’ll answer this step by step. I’ll tell you what was the desired result of the query (assignment given to us).

我们回到了最初的问题。 现在,我们将逐步回答此问题。 我将告诉您查询的期望结果是什么(分配给我们)。

Return all countries together with the number of related calls and their average duration in seconds. In the result display only countries where average call duration is greater than the average call duration of all calls.

返回所有国家/地区以及相关通话次数及其平均持续时间(以秒为单位)。 在结果显示中,仅显示平均通话时长大于所有通话的平均通话时长的国家。

The first thing we’ll do is to determine which tables we’ll be using in the process. In the data model, I’ve added colors to the tables we need to use.

我们要做的第一件事是确定在流程中将使用哪些表。 在数据模型中,我为需要使用的表添加了颜色。

the tables we'll use in the SELECT query

And how to determine which tables should be? The answer has two parts:

以及如何确定应该使用哪些表? 答案分为两个部分:

  • country (we need country_name) and 国家 (我们需要country_name)和call (we need start_time and end_time to calculate the average call duration) 调用 (我们需要start_time和end_time来计算平均通话时间)
  • country table to the 国家表到call table) 调用表)

After this analysis we know we must use the following tables: country, city, customer, and call. If we want to use them properly, we need to JOIN these tables using foreign keys. Without even thinking about the final query, we now know it will contain this part:

经过分析,我们知道必须使用下表: 国家城市客户电话 。 如果要正确使用它们,则需要使用外键JOIN这些表 。 现在甚至不用考虑最终查询,我们知道它将包含以下部分:

SELECT 
	...
FROM country 
LEFT JOIN city ON city.country_id = country.id
LEFT JOIN customer ON city.id = customer.city_id
LEFT JOIN call ON call.customer_id = customer.id
...;

We could do one thing, and that is to test what the query like this would return:

我们可以做一件事,那就是测试这样的查询将返回什么:

SELECT 
	*
FROM country 
LEFT JOIN city ON city.country_id = country.id
LEFT JOIN customer ON city.id = customer.city_id
LEFT JOIN call ON call.customer_id = customer.id;

I won’t post the picture of the whole result because it simply has too many columns. Still, you can check it. I always advise that you test parts of your queries. While they won’t be displayed in the final results, they will be used in the background. By testing these parts you’ll get the idea of what is happening in the background, and could assume what the final result should be. But still, we have to answer on “How to write a complex SELECT query?”.

我不会发布整个结果的图片,因为它只包含太多列。 不过,您可以检查一下。 我总是建议您测试部分查询。 尽管它们不会显示在最终结果中,但将在后台使用它们。 通过测试这些部分,您将了解后台发生的事情,并可以假设最终结果应该是什么。 但是,我们仍然必须回答“如何编写复杂的SELECT查询?”。

如何编写复杂的SELECT查询-当时编写查询的一部分 (How to Write a Complex SELECT Query – Write Parts of the Query at the Time)

We have already written part of the query and that’s a good practice. It will help you to build a complex query from simpler “blocks” but also, you’ll test your query along the way because you’ll be checking parts of it at a time as well, check how the query works when certain parts are added or executed.

我们已经编写了查询的一部分,这是一个好习惯。 它可以帮助您从较简单的“块”构建复杂的查询,而且还可以一路测试您的查询,因为您还将同时检查其中的一部分,检查某些部分是否存在时查询的工作方式添加或执行。

I would start with this part “where average call duration is greater than the average call duration of all calls”. It’s obvious that we need to calculate the average duration from all calls (in seconds). So let’s do that.

我将从“平均通话时长大于所有通话的平均通话时长”这一部分开始。 显然,我们需要计算所有通话的平均时长(以秒为单位)。 因此,让我们这样做。

SELECT AVG(DATEDIFF(SECOND, call.start_time, call.end_time)) FROM call

subquery result

We’ve explained the aggregate functions in the previous article. So far, we haven’t talked about date & time functions, but it’s enough to say that the DATEDIFF function calculates the difference in the units of the given time period (we are after seconds here) between the start time and end time. The result returned implies that the average call duration was 354 seconds.

我们已经在上一篇文章中解释了聚合函数 。 到目前为止,我们还没有讨论日期和时间函数,但是可以说DATEDIFF函数以给定时间段(此处为秒)为单位计算开始时间和结束时间之间的差。 返回的结果表明平均通话时间为354秒。

Now we’ll write down the query which returns aggregated values for all countries.

现在,我们将写下查询,该查询将返回所有国家/地区的汇总值。

SELECT 
	country.country_name_eng,
	SUM(CASE WHEN call.id IS NOT NULL THEN 1 ELSE 0 END) AS calls,
	AVG(ISNULL(DATEDIFF(SECOND, call.start_time, call.end_time),0)) AS avg_difference
FROM country 
LEFT JOIN city ON city.country_id = country.id
LEFT JOIN customer ON city.id = customer.city_id
LEFT JOIN call ON call.customer_id = customer.id
GROUP BY 
	country.id,
	country.country_name_eng
ORDER BY calls DESC, country.id ASC;

I would like to point out two things here:

我想在这里指出两件事:

  • LEFT JOIN, we’ll also join countries without any call. In case we’ve used COUNT, we would have value 1 returned for countries without any call, and we want 0 there (we want to see that info) LEFT JOIN ,因此我们也将在不打任何电话的情况下加入国家。 如果我们使用了COUNT,则对于未打任何电话的国家/地区,我们将返回值1,并且我们希望在该处返回0(我们想查看该信息)
  • AVG(ISNULL(DATEDIFF(SECOND, call.start_time, call.end_time),0)) – This is very similar to the previously mentioned AVG. The difference here is that I’ve used ISNULL(…,0). This simply tests if the calculated value IS NULL, and if so, replaces it with 0. Calculated value could be NULL if there is not data (we’ve used LEFT JOIN)

    AVG(ISNULL(DATEDIFF(SECOND,call.start_time,call.end_time),0))–这与前面提到的AVG非常相似。 此处的区别在于,我使用了ISNULL(…,0)。 这只是测试计算值是否为NULL,如果是,则将其替换为0。如果没有数据,则计算值可以为NULL(我们使用了LEFT JOIN)

Let’s see what this query returns.

让我们看看该查询返回什么。

complex SELECT query without HAVING

“How to write a complex SELECT query?” -> Now we’re really close to complete our query and get really close to this answer.

“如何编写复杂的SELECT查询?” ->现在,我们真的很接近完成查询并非常接近这个答案。

So, the result contains all countries with their number of calls and the average call duration. From this result, we’re interested only in these having average call duration greater than average call duration of all calls. That’s our original query, but with comments added.

因此,结果包含所有国家/地区及其通话次数和平均通话时间。 从这个结果来看,我们只对平均通话时间长于所有通话的平均通话时间的那些感兴趣。 这是我们的原始查询,但添加了注释。

-- the query returns a call summary for countries having average call duration > average call duration of all calls
SELECT 
    country.country_name_eng,
    SUM(CASE WHEN call.id IS NOT NULL THEN 1 ELSE 0 END) AS calls,
    AVG(ISNULL(DATEDIFF(SECOND, call.start_time, call.end_time),0)) AS avg_difference
FROM country 
-- we've used left join to include also countries without any call
LEFT JOIN city ON city.country_id = country.id
LEFT JOIN customer ON city.id = customer.city_id
LEFT JOIN call ON call.customer_id = customer.id
GROUP BY 
    country.id,
    country.country_name_eng
-- filter out only countries having an average call duration > average call duration of all calls
HAVING AVG(ISNULL(DATEDIFF(SECOND, call.start_time, call.end_time),0)) > (SELECT AVG(DATEDIFF(SECOND, call.start_time, call.end_time)) FROM call)
ORDER BY calls DESC, country.id ASC;

You can see the query result in the picture below.

您可以在下图中看到查询结果。

How to Write a Complex SELECT Query - final query with comments

Compared to the previous query, we’ve just added the HAVING part. While in the WHERE part of the query we test “regular” values, HAVING part of the query is used to test aggregated values. We’re using it to compare AVG values.

与上一个查询相比,我们仅添加了HAVING部分。 在查询的WHERE部分中,我们测试“常规”值,而查询的HAVING部分中,则用于测试汇总值。 我们正在使用它来比较AVG值。

Comments are a crucial thing, not only in databases but in programming in general. By adding these 3 comment lines, the query should become much more readable. Even somebody who looks at this query for the first time will see what you did and why. That somebody could even be you if you’re looking at the code you wrote some time ago. While it takes some time to write these comments, don’t be lazy and do it. You’ll probably save yourself much more time when revisiting old queries/code.

注释不仅在数据库中,而且在一般编程中都是至关重要的。 通过添加这3条注释行,查询应变得更具可读性。 即使是第一次浏览此查询的人,也会看到您的操作以及原因。 如果您正在查看您前一段时间编写的代码,那么甚至有人可能是您。 尽管写这些评论要花一些时间,但不要偷懒去做。 重访旧查询/代码时,您可能会节省更多时间。

Let’s Wrap up Everything

让我们总结一切

So, the question was – “How to write a complex SELECT query?”. While there is no easy answer, I would suggest the following steps:

因此,问题是–“如何编写复杂的SELECT查询?”。 虽然没有简单的答案,但我建议采取以下步骤:

  • Think of it as of LEGO bricks and build the query that way. Treat complex parts as black boxes – they will return what they need to and you’ll write (and incorporate into the main query) them later

    将其视为LEGO积木,并以此方式构建查询。 将复杂的部分视为黑盒–它们将返回所需的内容,稍后您将编写它们(并将其合并到主查询中)
  • Identify all the tables you’ll need in the query

    识别查询中需要的所有表
  • Join tables containing the data you need to display or the data used in the WHERE part of the query

    连接包含您需要显示的数据或查询的WHERE部分中使用的数据的表
  • Display all data to check if you’ve joined everything correctly and to see the result of such a query

    显示所有数据以检查您是否正确连接了所有内容,并查看查询结果
  • Create all subqueries separately. Test them to see do they return what they should. Add them to the main query

    分别创建所有子查询。 测试他们,看他们是否返回了应有的状态。 将它们添加到主查询
  • Test everything

    测试一切
  • Add comments

    添加评论

Could you give us your answer on “How to write a complex SELECT query?”. Which approach have you used?

您能否回答“如何编写复杂的SELECT查询?”。 您使用了哪种方法?

目录 (Table of contents)

Learn SQL: CREATE DATABASE & CREATE TABLE Operations
Learn SQL: INSERT INTO TABLE
Learn SQL: Primary Key
Learn SQL: Foreign Key
Learn SQL: SELECT statement
Learn SQL: INNER JOIN vs LEFT JOIN
Learn SQL: SQL Scripts
Learn SQL: Types of relations
Learn SQL: Join multiple tables
Learn SQL: Aggregate Functions
Learn SQL: How to Write a Complex SELECT Query
Learn SQL: The INFORMATION_SCHEMA Database
Learn SQL: SQL Data Types
Learn SQL: Set Theory
Learn SQL: User-Defined Functions
Learn SQL: User-Defined Stored Procedures
Learn SQL: SQL Views
Learn SQL: SQL Triggers
Learn SQL: Practice SQL Queries
Learn SQL: SQL Query examples
Learn SQL: Create a report manually using SQL queries
Learn SQL: SQL Server date and time functions
Learn SQL: Create SQL Server reports using date and time functions
Learn SQL: SQL Server Pivot Tables
Learn SQL: SQL Server export to Excel
Learn SQL: Intro to SQL Server loops
Learn SQL: SQL Server Cursors
Learn SQL: SQL Best Practices for Deleting and Updating data
Learn SQL: Naming Conventions
学习SQL:CREATE DATABASE&CREATE TABLE操作
学习SQL:插入表
学习SQL:主键
学习SQL:外键
学习SQL:SELECT语句
学习SQL:INNER JOIN与LEFT JOIN
学习SQL:SQL脚本
学习SQL:关系类型
学习SQL:联接多个表
学习SQL:聚合函数
学习SQL:如何编写复杂的SELECT查询
学习SQL:INFORMATION_SCHEMA数据库
学习SQL:SQL数据类型
学习SQL:集合论
学习SQL:用户定义的函数
学习SQL:用户定义的存储过程
学习SQL:SQL视图
学习SQL:SQL触发器
学习SQL:练习SQL查询
学习SQL:SQL查询示例
学习SQL:使用SQL查询手动创建报告
学习SQL:SQL Server日期和时间函数
学习SQL:使用日期和时间函数创建SQL Server报表
学习SQL:SQL Server数据透视表
学习SQL:将SQL Server导出到Excel
学习SQL:SQL Server循环简介
学习SQL:SQL Server游标
学习SQL:删除和更新数据SQL最佳实践
学习SQL:命名约定

翻译自: https://www.sqlshack.com/learn-sql-how-to-write-a-complex-select-query/

复杂sql 查询编写方法

从零开始的立绘拆教程 蓝毒小天使天下第一! 本文拆教程不限于明日方舟,在后面也会给出其他手游的拆教程,例如少女前线 首先拆最简单的无非就是拆取游戏资源,例如游戏立绘,音频,视频等,再深层次一点有拆取游戏配置文件,apk反译得到部分源 在本文,我们只介绍拆取游戏资源和对游戏资源的后期处理(因为另外两个讲道理我也没太搞懂,虽然可以提取游戏配置文件,但似乎大部分重要的数据都有加密,反译出来的源也只是部分,... 阅读详情

相关推荐

Altair HyperWorks 2022.3.0 Suite下载

Altair HyperWorks开发团队提供了新的一流技术,以设计和优化高性能、高效和创新的产品,并发布了HyperWorks 2021.2套件。与所有新版本一样,这个版本含了新特性、增强功能,并修复了前一个版本中用户报告的各种问题,提供了总体上更稳定的体验。链接: https://pan.baidu.com/s/1rYietmQLQUboHiJzYoE9WQ?pwd=m9yh 提取: m9yh 复制这段内容后打开百度网盘手机App,操作更方便哦。

weixin_48469365的博客 2003

SQL 复杂查询

SQL 复杂查询指的就是子查询。为什么子查询叫做复杂查询呢?因为子查询相当于查询嵌套查询,因为嵌套导致复杂度几乎可以被无限放大(无限嵌套),因此叫复杂查询。下面是一个最简单的子查询例子:S...

前端精读周刊 5822

为什么 CL_DEMO_OUTPUT 不能用于生产用途:不仅是 demo 标签那么简单

摘要:CL_DEMO_OUTPUT 虽能快速展示ABAP数据,但存在严重生产风险:1) 官方定位为示例工具,不承诺兼容性与支持;2) 非Released API,云环境直接不可用;3) 依赖SAP GUI,缺乏日志关键特性;4) 存在版本漂移、类型限制、性能隐患及安全缺口。典型事故案例括后台Job崩溃、Gateway链路污染和云迁移失败。生产环境应改用结构化日志(如SLG1)或稳定API输出,避免将调试工具误作工程方案。(149字)

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 103

每日10行代51:复杂sql查询的一些经验

这两天又了不少sql脚本,总结这些年复杂查询的经验。由于我一般一次性分析类脚本,下面经验也主要针对这种脚本。经验基于oracle. 查询要分清是经常使用还是一次性的分析使用,如果是长期使用要注意下效率,但如果是一次性分析使用,可以不那么在乎效率,怎么方便怎么来,当然如果跑的太慢也还是要想办法优化的。 如果有建表的权限,可以建立临时表来拆分复杂查询。这样不仅语句好,执行速度也快,还便于调试,免得出现一个复杂查询跑几个小时,结果发现是错的,又要去调试。 数据量大时,先用少量数据甚至单条数据来测试,

天天卡丁的博客 717

复杂SQL编写

复杂SQL编写

I______F的博客 572

数据库复杂sql如何编写入手

说到数据库我想大家都不陌生,对主流的数据库都会基本使用,但是要sql完成复杂sql编写是需要对数据库原理,sql脚本语法有一定的了解的,但是对于开发人员来说,平常都是在curd一些业务代数据库接触的也不是那么复杂,对于复杂的业务场景,面对sql显然束手无策,对于后端开发人员来说去看一个几百行,几千行的sql实在头大,

小杨互联网 1790

@Select中较为复杂法集锦与注意事项

@Select中较为复杂法集锦与注意事项

qq3892997的博客 2068

select查询中@作用_SQL学习第四关:复杂查询

一、视图1.视图创建练习course表CREATE VIEW 每个学生课程平均分(学号,平均分) as select 学号,AVG(成绩) from course GROUP BY 学号;运行结果视图可以看作定义在MYSQL上的的临时表,是另一种查看数据的入口。视图本身并不存储实际的数据,而仅仅是由SELECT语句组成的查询来定义的临时表 。视图就如同一张表一样,对表能够进行的操作都可以应用于视图...

weixin_39789042的博客 731

SQL SERVER专题实验4 复杂查询

本关主要介绍的是概念性知识,因此测试以选择题的形式对本关介绍的知识进行测试。请参考后面的正确运行结果,使用嵌套查询的方案T-SQL语句。请参考后面的正确运行结果,使用集合查询方案T-SQL语句。5、关于基于派生表的查询,下面说法正确的有:(ABD)3、关于嵌套查询,下面说法正确的有:(ABCD)4、关于集合查询,下面说法正确的有:(ABCD)1、关于自身连接,下面说法正确的有:(ABC)2、关于自然连接,下面说法正确的有:(ABC)请参考后面的正确运行结果T-SQL语句。

qq_45326829的博客 1万+

MySQL SELECT 查询(二):复杂查询的实现

MySQL SELECT 查询(二):复杂查询的实现,括多表查询常见错误与连接规范,SQL99连接新特性,其中常见的SQL JOIN,流程控制与高级功能if ifnull数,case when..,case.. when以及加密解密的使用,UNION 与 UNION ALL,SQL 查询执行过程,子查询的广泛应用与常见问题解析

2301_77207909的博客 3530

学习篇】SQL复杂查询学习

摘要: 本文探讨MySQL千万级大表的优化策略,重点围绕复杂查询与性能调优展开。首先解析SQL执行顺序和表关系(多对多、一对多、一对一),介绍子查询和JOIN操作的原理与应用场景。进阶部分涵盖分组聚合、窗口数等高级查询技术。性能优化方面强调索引设计(B树/哈希索引)、字段数据类型选择和视图的使用,避免全表扫描。文章还指出不合理的索引可能导致存储开销增加,建议根据实际查询需求和数据特性平衡索引策略。通过合理的表结构设计、查询优化技巧和资源管理,可显著提升大规模数据处理的效率。

Logintern09的博客 1934

SQL语句详解:SELECT查询的艺术

子句主要作用最佳使用场景WHERE在数据源级别过滤行需要减少参与计算的数据量时GROUP BY按指定列对结果分组需要汇总统计数据时HAVING过滤分组后的结果需要基于聚合结果筛选时ORDER BY对结果集排序需要按特定顺序展示数据时LIMIT限制返回的行数分页或只需要部分结果时窗口数(又称分析数)是SQL中的高级特性,允许我们在不改变结果集行数的情况下进行计算,这就像是给每行数据增加了"上下文感知"能力。-- 窗口数基本语法SELECT窗口数的核心组成部分。

sinat_27016095的博客 1684

一个复杂sql语句

$sp_sql = "select sp_ProductNo, sp_ProductName,sp_Standard,sp_Unit,sum(sp_Amount) as amount from rd_store_product where sp_Id in (select wv_Id from rd_warehouse_voucher where wv_StoreHouse...

weixin_30768661的博客 153

如何复杂SQL

经常有人问我那非常复杂sql是怎么出来的,我一直不知道该怎么回答。 因为虽然我这样的sql很顺手,可是我却不知道怎么告诉别人怎么。 很多人将这个问题归结为天赋,我却不这么看,我想这个不是天赋的问题, 任何人经过一定有效率的学习和练习都能完成。有的人可能学习的快点,有的 人可能学习的慢点,这个的确跟每个人有关,但只要经过有规律的练习,我觉得 还是能够很快的出符合要求的sql的。我也一直认为,不知道怎么是因为没有

weixin_33465519的博客 1780

复杂SQL编写要领

SQL显得越来越重要,原因是很多逻辑处理,都可以跟sql挂钩。 个人排斥将逻辑层代复杂化, 因此sql在未来的项目架构中,举足轻重, 特地开一章节, 来总结一下以往项目中sql的精髓部分。【待完成】

X-Teamer提炼商业万有引力模型, 低成本定制化开发 3962

复杂SQL语句的书(mybatis中XML文件的核心)

select sd.name as dptName, pro.`name` as provinceName, c.`name` as cityName, a.`name` as areaName, info.id, dpt.*, info.*, gd.* from info_sys as info left join depart as ...

wyqwilliam的博客 4419

复杂SQL

【代复杂SQL

xiaochenjam的博客 287

由浅入深的SQL语句

  突然间来了兴趣,想整理一下由初步至较为复杂SQL法,下面的东西是想到哪儿到哪儿,每一个层级的难度都会上升一点,大家凑合着看吧   假设有一张表TABLEA和表TABLEB,它们都各自有三个字段ID(自增长)、NAME(姓名)、SCORE(分数)、CREATEDATE(录入时间),其中两张表格可以通过ID进行关联 1、基本语句 1.1、查询 SELECT ID,NAME FROM TABLEA WHERE NAME LIKE '%TEST%' ORDER BY ID DESC 1.2、修改

wxl847466025的博客 324

一些复杂sql处理

select 'select '''||partition_name||''' partition_name,min(主键列) flag from '||table_owner||'.'||table_name||' partition ('||partition_name||') union all ' from dba_tab_partitions where table_name='XXX';update 表名 set 字段=null where 字段=某 --即将表中字段为某的替换为null。

2301_76787626的博客 1406

mysql view在测试过程的应用

两个有依赖的系统,位于不同的库中,通过跨库连表形成一个虚拟表,从而方便我进行查询 view视图 要学习视图,我们可以尝试解答下面三个问题 到底什么是视图? 视图就是一个虚拟的表,甚至可以理解为一个select语句 如何创建视图? 如何删除视图? 使用视图,可以简化数据操作,你想想正常情况下你要left join才能把两个表连接起来,可是通过视图相当于访问封装好的数,你说视图好不好用。 视图是...

叶子常常随风而落,分享博主日常学习和使用的一些技术 320

复杂SQL优化实例

为 JOIN 之后,子查询的选择模式从 DEPENDENT SUBQUERY 变成 DERIVED,执行速度大大加快,从7秒降低到2毫秒。比如下面 UPDATE 语句,MySQL 实际执行的是循环/嵌套子查询(DEPENDENT SUBQUERY),其执行时间可想而知。这种法不仅存在额外的开销,还使得整个语句显的繁杂。在前端数据浏览翻页,或者大数据分批导出等场景下,是可以将上一页的最大当成参数作为查询条件的。不难看出子查询 c 是全表聚合查询,在表数量特别大的情况下会导致整个语句的性能下降。

qq_16570607的博客 1357

STM32F10x系列标准固件库(V3.6.0)

STM32F10x系列标准固件库(V3.6.0)

【大学生电子设计资料】:400HZ中频电源设计毕业论文资料.rar

【大学生电子设计资料】:400HZ中频电源设计毕业论文资料.rar

上一篇: sql注入利用_SQL注入:这是什么? 原因和利用
下一篇: 创建视图SQL:在SQL Server中修改视图
culuo4781
博客等级 码龄10年 311粉丝 0原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值