简体   繁体   English

将多个查询合并为一个

[英]Combining multiple queries into one

Is it possible to join multiple querys in one? 是否可以将多个查询合而为一?

I have 2: 我有2个:

SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
FROM Table
WHERE Color = 'Red'

and

SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
FROM Table
WHERE Color = 'Blue'

Now i want both in one query 现在我要两个查询

So is try like this: 所以是这样尝试:

SELECT
(
SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
FROM Table
WHERE Color = 'Red'
)
(
SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff2
FROM Table
WHERE Color = 'Blue'
)

But doenst work :( 但是要做好工作:(

You could use a single query with IN operator. 您可以使用带有IN运算符的单个查询。 This is a short for multiple OR conditions: 这是多个OR条件的缩写:

SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
FROM Table
WHERE Color IN ( 'Red', 'Blue' )

If you really insist on (discouraged) having two queries, then use UNION ALL to combine them: 如果您真的坚持(劝阻)使用两个查询,则可以使用UNION ALL组合它们:

SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
FROM Table
WHERE Color = 'Red'
UNION ALL -- does not remove duplicates from output
SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
FROM Table
WHERE Color = 'Blue'

If you want to remove duplicates use UNION instead of UNION ALL . 如果要删除重复项,请使用UNION而不是UNION ALL


If you need result in different columns as mentioned in a comment then use a CASE statement for that. 如果您需要在注释中提到不同的结果列,请为此使用CASE语句。 I really don't see a reason for this though. 不过,我确实没有找到原因。

SELECT 
  CASE WHEN Color = 'Red' THEN DATEDIFF(CURRENT_DATE, cl.updated_at) END AS Diff,
  CASE WHEN Color = 'Blue' THEN DATEDIFF(CURRENT_DATE, cl.updated_at) END AS Diff2,
FROM Table
WHERE Color IN ( 'Red', 'Blue' )

And for second approach: 对于第二种方法:

SELECT
  CASE WHEN Color = 'Red'  THEN Diff END AS Diff,
  CASE WHEN Color = 'Blue' THEN Diff END AS Diff2
FROM (
  SELECT Color, DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
  FROM Table
  WHERE Color = 'Red'
  UNION ALL -- does not remove duplicates from output
  SELECT Color, DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
  FROM Table
  WHERE Color = 'Blue'
) t

Isn't it just updating the where in this situation? 在这种情况下,它不只是更新where吗?

SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff FROM Table WHERE Color = 'Blue' OR Color = 'red'

And yes you can use an INNER JOIN or UNION, but for this example, that wouldn't be useful. 是的,您可以使用INNER JOIN或UNION,但是对于此示例,这将无用。

You can use the UNION operator to Join your two queries. 您可以使用UNION运算符来联接两个查询。 https://www.w3schools.com/sql/sql_union.asp https://www.w3schools.com/sql/sql_union.asp

If your output column is same in all your query then you can use UNION / UNION ALL like below: 如果所有查询中的输出列都相同,则可以使用UNION / UNION ALL如下所示:

SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
FROM Table
WHERE Color = 'Red'

UNION

SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
FROM Table
WHERE Color = 'Blue'

If you can live with the values in different rows, then just do: 如果可以使用不同行中的值,则只需执行以下操作:

SELECT Color, DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
FROM Table
WHERE Color IN ('Red', 'Blue');

I added Color to the SELECT so you can distinguish the rows. 我将Color添加到SELECT以便您可以区分行。

If the subqueries return one value, then you are simply missing a comma: 如果子查询返回一个值,则您只是缺少逗号:

SELECT (SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff
        FROM Table
        WHERE Color = 'Red'
       ),
       (SELECT DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff2
        FROM Table
        WHERE Color = 'Blue'
       );

If the subqueries return multiple rows, then you need to explain the result set you want. 如果子查询返回多行,则需要解释所需的结果集。

EDIT: 编辑:

If you want the values to line up in two different columns, then you can to do something like this: 如果您希望这些值在两个不同的列中对齐,则可以执行以下操作:

select max(diff_red) as diff_red, max(diff_blue) as diff_blue
from ((select (@rnr := @rnr + 1) as rn, 
              DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff_red,
              NULL AS Diff_blue
       from t cross join
            (select @rnr := 0) params
       where color = 'Red'
      ) union all
      (select (@rnb := @rnb + 1) as rn, 
              NULL AS Diff_red,
              DATEDIFF(CURRENT_DATE, cl.updated_at) AS Diff_blue
       from t cross join
            (select @rnrb:= 0) params
       where color = 'Blue'
      )
     ) rb
group by rn;

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

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