user655071
user655071

Reputation: 31

MySQL multiple SELECT statements

I need to use several SELECT statements in one statement. I've checked out some other questions and figured that this should work:

SELECT (SELECT users.fname, users.lname, posts.post
          FROM users, posts, comments
         WHERE users.userid = posts.userid)
       (SELECT users.fname, users.lname, comments.text
          FROM users
         WHERE comments.userid = users.userid
           AND posts.postid = comments.postid)

However, it doesn't work... help!

Upvotes: 2

Views: 8738

Answers (1)

Frank Schmitt
Frank Schmitt

Reputation: 30775

Assuming you want a list of all users that have either posted or left a comment, UNION ALL is what you want (I changed the FROM/WHERE clauses accordingly):

SELECT users.fname, users.lname, posts.post
FROM users, posts
WHERE users.userid = posts.userid
UNION ALL
SELECT users.fname, users.lname, comments.text
FROM users, comments
WHERE comments.userid = users.userid

Upvotes: 5

Related Questions