Fuxi
Fuxi

Reputation: 7589

MySQL Query - a bit tricky

i'm having the following tables:

USERS:                               ITEMS

user_id  name    always_show         id   user_id  name
--------------------------           --------------------------------
1        Joe                         1    1        Apple   
2        Sam                         2    1        Banana
3        Walter  1                   3    2        Cherry

i'm doing this query:

SELECT * FROM
  users Inner Join
  items On users.user_id = items.user_id

The question is: how can i add all other users which have the flag always_show set?

Upvotes: 0

Views: 57

Answers (3)

StevieG
StevieG

Reputation: 8709

SELECT * 
FROM users 
INNER JOIN items On users.user_id = items.user_id
UNION
SELECT * 
FROM users 
LEFT OUTER JOIN items On users.user_id = items.user_id AND users.always_show = 1

Upvotes: 0

silly
silly

Reputation: 7887

i hope i understand your question :-) try UNION

SELECT u.* FROM users u
INNER JOIN items i ON u.user_id = i.user_id

UNION ALL SELECT * FROM users WHERE always_show = 1

Upvotes: 1

app_dev89
app_dev89

Reputation: 53

SELECT * FROM
  users Inner Join
  items On users.user_id = items.user_id where Users.always_set = 1

Upvotes: 0

Related Questions