Search code examples
mysqljoindual-table

MySQL Dual table Join Dual table


Is there a way to JOIN fields from the dummy DUAL table in MySQL?

Say I have a

SELECT 1 as n1 FROM DUAL

n1
--
 1

and a

SELECT 2 as n2 FROM DUAL

n2
--
 2

Can I join both selects with a Join in some kind of query as...

SELECT 1 as n1 FROM DUAL JOIN (SELECT 2 as n2 FROM DUAL) ON 1=1

?


Solution

  • Here's one way...

    Select t1.n1, t2.n2 
    from (select 1 as n1) t1 
    CROSS JOIN (Select 2 as n2) t2;
    

    Here's another

    Select t1.n1, t2.n2 
    from (select 1 as n1, 'A' as ID) t1 
    INNER JOIN (Select 2 as n2, 'A' as ID) t2
      on T1.Id = T2.ID;
    

    and you could just do

    Select 1 as n1, 2 as n2
    

    but I assume there's a reason you need the joins.