Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL subquery or not?

I am trying to find a query that will select from two tables but not join those two tables as I don't want the result on one line.

Lets say I have these tables:

Persons1                 Persons2
Number Name    Surname   Number  Name   Surname
-----------------------  ------------------------- 
1      Peter   Miller    1       Frank  Farian 
2      Hans    Geige     2       Thomas Müller

And when I use this query:

SELECT
  NEW.NAME,
  OLD.NAME
FROM
  PERSONS1 NEW,
  PERSONS2 OLD
WHERE
  NEW.Number = 1 AND
  OLD.Number = 1

I get the following result:

Peter   Frank

How do I write the query to get:

Peter
Frank
like image 719
Michael Müller Avatar asked Feb 11 '23 04:02

Michael Müller


1 Answers

Use UNION for this:

SELECT NEW.NAME
FROM PERSONS1 NEW
WHERE NEW.Number = 1
UNION 
SELECT OLD.NAME
FROM PERSONS2 OLD
WHERE OLD.Number = 1
like image 153
dotnetom Avatar answered Feb 13 '23 03:02

dotnetom