简体   繁体   English

将查询结果传递给子查询

[英]Passing query result into subquery

SELECT alert,  
  (select created_at from alerts 
    WHERE alert = @ALERT ORDER BY created_at desc LIMIT 1) 
      AS latest FROM alerts GROUP BY alert; 

I am having an issue with the above query where I would like to pass in each alert into the subquery so that I have a column called latest which displays the latest alert for each group of alerts. 我在上面的查询中遇到了一个问题,我想将每个警报传递到子查询中,以便我有一个名为“最新”的列,该列显示每组警报的最新警报。 How should I do this? 我应该怎么做?

This is called a correlated subquery. 这称为相关子查询。 To make it work, you need table aliases: 要使其正常工作,您需要表别名:

SELECT a1.alert,  
       (select a2.created_at
        from alerts a2
        WHERE a2.alert = a1.alert
        ORDER BY a2.created_at desc
        LIMIT 1
       )  AS latest
FROM alerts a1
GROUP BY a1.alert; 

Table aliases are a good habit to get into, because they often make the SQL more readable. 表别名是一种很好的习惯,因为表别名通常会使SQL更具可读性。 It is also a good idea to use table aliases with column references, so you easily know where the column is coming from. 将表别名与列引用一起使用也是一个好主意,因此您可以轻松地知道列的来源。

EDIT: 编辑:

If you really want the latest, you can get it by simply doing: 如果您真的想要最新的,可以通过以下操作获得它:

select alert, max(created_at)
from alerts
group by alert;

I would do the following 我会做以下

SELECT 
    alert_group_name, 
    MAX(created_at) AS latest 
FROM 
     alerts A 
GROUP BY 
     alert_group_name; 

If you are trying to get the latest created_at date for each group of alerts, there is a simpler way. 如果您尝试获取每组警报的最新created_at日期,则有一种更简单的方法。

SELECT
    alert,
    max (created_at) AS latest
FROM
    alerts
GROUP BY
    alert;

For a correlated subquery, you need to reference an expression from the outer query. 对于相关子查询,您需要引用外部查询中的表达式。

The best way to do that is to assign an alias to the table on the outer query, and then reference that in the inner query. 最好的方法是在外部查询中为表分配别名,然后在内部查询中引用该别名。 Best practice is to assign an alias to EVERY table reference, and qualify EVERY column reference. 最佳实践是为每个表引用分配一个别名,并限定每个列引用。

All that needs to be done to "fix" your query is to replace the reference to " @ALERT " with a reference to the alert column from the table on the outer query. 要“修复”查询,所需要做的就是用对外部查询表中alert列的引用替换对“ @ALERT ”的引用。

In our shop, that statement would be formatted something like this: 在我们的商店中,该语句的格式如下:

SELECT a.alert
     , (SELECT l.created_at 
          FROM alerts l
         WHERE l.alert = a.alert 
         ORDER BY l.created_at DESC
         LIMIT 1
       ) AS latest
  FROM alerts a 
 GROUP
    BY a.alert

Not because that's easier to write that way, but more importantly it's easier to read and understand what the statement is doing. 不是因为那样写起来更容易,而是更重要的是,它更易于阅读和理解语句的作用。

The correlated subquery approach can be efficient for a small number of rows returned (a very restrictive WHERE clause on the outermost query.) But in general, correlated subqueries in the SELECT list can make for a (what we refer to in our shop) an LDQ "light dimming query". 相关子查询方法对于返回的少量行(在最外面的查询上非常严格的WHERE子句)可能是有效的。但是通常,SELECT列表中的相关子查询可以使(在我们的商店中引用) LDQ“调光查询”。

In our shop, if we needed the resultset returned by that query, that statement would likely be rewritten as: 在我们的商店中,如果我们需要该查询返回的结果集,则该语句可能会被重写为:

SELECT a.alert
     , MAX(a.created_at) AS latest
  FROM alerts a
 GROUP
    BY a.alert

And we'd definitely have an index defined ON alerts(alert,created_at) (or an index with additional columns after those first two.) size, we 而且,我们肯定会在ON alerts(alert,created_at)定义一个索引(或在前两个索引之后具有其他列的索引)。

(I don't anticipate any cases where this statement would return a different result.) (我预计此语句不会返回不同的结果。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM