将多个查询合并为一个

时间:2018-06-17 11:57:44

标签: mysql sql

是否可以在一个中加入多个查询?

我有2:

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

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

现在我想在一个查询中

所以尝试这样:

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'
)

但最重要的是:(

5 个答案:

答案 0 :(得分:2)

您可以对IN运算符使用单个查询。这是多个OR条件的缩写:

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

如果您真的坚持(气馁)有两个查询,请使用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'

如果您想删除重复项,请使用UNION代替UNION ALL

如果您需要在评论中提到不同列的结果,那么请使用CASE语句。我真的没有看到这个原因。

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' )

第二种方法:

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

答案 1 :(得分:1)

是不是只是在这种情况下更新where

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

是的,你可以使用INNER JOIN或UNION,但对于这个例子,这没用。

答案 2 :(得分:0)

您可以使用UNION运算符加入您的两个查询。 https://www.w3schools.com/sql/sql_union.asp

答案 3 :(得分:0)

如果您的所有查询中的输出列都相同,那么您可以使用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'

答案 4 :(得分:0)

如果您可以使用不同行中的值,那么只需执行:

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

我将Color添加到SELECT,以便您区分行。

如果子查询返回一个值,那么您只是缺少一个逗号:

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'
       );

如果子查询返回多行,则需要解释所需的结果集。

编辑:

如果您希望值在两个不同的列中对齐,那么您可以执行以下操作:

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;