Dubstaphone
Dubstaphone

Reputation: 418

How do I combine the results of two queries in SQL?

My bad if I don't understand simple things, as I am just beginning to write SQL queries.

I have two queries:

SELECT * FROM status WHERE author IN (SELECT user1 FROM friends WHERE user2='$username' AND accepted='1') OR author IN (SELECT user2 FROM friends WHERE user1='$username' AND accepted='1')

and

SELECT * FROM status WHERE author = '$username'

How can I combine the results of these two queries, either natively in the SQL query, or in PHP?

Upvotes: 0

Views: 77

Answers (2)

JunM
JunM

Reputation: 7160

Try UNION:

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

To allow duplicate values, use the ALL keyword with UNION.

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

Upvotes: 4

sgeddes
sgeddes

Reputation: 62861

What's wrong with just using OR?

SELECT * 
FROM status 
WHERE author IN (SELECT user1 FROM friends WHERE user2='$username' AND accepted='1') 
    OR author IN (SELECT user2 FROM friends WHERE user1='$username' AND accepted='1')
    OR author = '$username'

Upvotes: 1

Related Questions