0

Is it possible to join multiple querys in one?

I have 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 :(

2
  • 2
    You haven't described the expected output.And I would have thought the answer you accepetd earlier with some trivial modifications might do Commented Jun 17, 2018 at 12:02
  • Expected out are 2 different columns Commented Jun 17, 2018 at 12:47

5 Answers 5

2

You could use a single query with IN operator. This is a short for multiple OR conditions:

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:

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.


If you need result in different columns as mentioned in a comment then use a CASE statement for that. 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
Sign up to request clarification or add additional context in comments.

6 Comments

It would also be useful to add Color to columns in SELECT, so we know which difference relates to which color.
Thanks for typo correction. Sure, definitely, I just don't see it in OP's query but normally that would be wise, depending on where you want to use that later.
I should have said, that i have oversimplyfied this WHERE, in my case its pretty complex and i also need 2 diffrent columns as output
Updated my answer. If that's not what you need, then your question is unclear.
Im sorry i try to be more specific. i need a way to easy put my 2 query together, because the are both very long and complex and i have simplyfied my example. case when might work, but i would need a week to recode this...
|
1

Isn't it just updating the where in this situation?

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.

2 Comments

This will return 0 rows. Color can't be Blue and Red at the same time :-)
That's correct! I should use the OR statement, thanks I will update my answer.
0

You can use the UNION operator to Join your two queries. https://www.w3schools.com/sql/sql_union.asp

Comments

0

If your output column is same in all your query then you can use UNION / UNION ALL like below:

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 Comments

I need 2 differnt columns
Why you want 2 different columns? You can include a column for colors.
Because its for 2 different statistics and my example is simplified ;)
Then add sample data, expected result, because it's unclear.
0

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.

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;

2 Comments

I just thought this would fix it, but i still get Error 1242 (Subquery returns more than 1 row), could this be because im joining multiple tables in each? (same for each)
@Intercept0r . . . As the comments requested, you should provide sample data and desired results. We cannot read your mind about what your data looks like. I edited the answer with what you presumably need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.