bergman
bergman

Reputation: 185

Select Where In Join

I want to get results from ids i already know. This is my attempt(pseudo):

SELECT * 
FROM main 
LEFT JOIN child 
WHERE main.id IN("1,2,3,4") 
ON main.id = child.main_id

How to get the childs from main by known ids from WHERE IN?

thx for help

Upvotes: 0

Views: 6150

Answers (3)

Minoru
Minoru

Reputation: 1730

The first problem is that you're putting a String in the IN clausule, instead of a list of items.

The second problem is that the ON clausule must be right after the JOIN clausule, I guess. You can try it two different ways (I think you're kinda mixing them!):

SELECT * FROM main m, child c 
    WHERE m.id IN(1,2,3,4) AND c.main_id = m.id

OR

SELECT * FROM main m LEFT JOIN child c ON m.id = c.main_id 
    WHERE main.id IN(1,2,3,4)

I hope this helps.

Upvotes: 0

Parixit
Parixit

Reputation: 3855

Please try this, I think you have wrong syntax for what you need.

SELECT * FROM main LEFT JOIN child ON main.id = child.main_id WHERE main.id IN(1,2,3,4)

ON should be right after JOIN of both table and IN condition have no " wrapping around it.

Upvotes: 1

Angelo Saleh
Angelo Saleh

Reputation: 622

I think what you are trying to get could be reached by 2 options:

1.

SELECT * FROM main LEFT JOIN child ON main.id = child.main_id WHERE main.id IN(1,2,3,4);

remove the quotations.

or

2.

SELECT * FROM main JOIN child ON main.id = child.main_id AND main.id IN(1,2,3,4);

Upvotes: 0

Related Questions