开发者

Join table query

If I join two tables in a query that looks:

SELECT m1.id AS reg, m1.name AS name, f.registered AS status 
FROM phone f 
INNER JOIN members m1 ON m1.id=f.user_id 
WHERE m1.status='1' AND f.registered='1'

And then I want to add another 10 user ID's in a array like m1.id IN (014,01284,014829,075090) that should also be listed in the result of a query. I want to avoid a third table in the query because I already know users from this table that I need.

The point is that the end result contains all the detail's of users get from the members table by ID's listed in phone table and array.

What开发者_运维技巧 the best way to do this?


Seems a bit trivial, so maybe not what you need, but like this?

SELECT m1.id AS reg, m1.name AS name, f.registered AS status 
FROM phone f 
INNER JOIN members m1 ON m1.id=f.user_id 
WHERE 
 (m1.status='1' AND f.registered='1')
OR
 (m1.id IN (014,01284,014829,075090) )


The INNER JOIN restricts all your results to those with a match in the join table, but that doesn't sound like what you want

I think Nanne's answer will work if you change INNER JOIN to LEFT JOIN

SELECT m1.id AS reg, m1.name AS name, f.registered AS status 
FROM phone f 
LEFT JOIN members m1 ON m1.id=f.user_id 
WHERE 
 (m1.status='1' AND f.registered='1')
OR
 (m1.id IN (014,01284,014829,075090) )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜