Jump to content

CASE in WHERE statement


Destramic

Recommended Posts

i've read and seen there are variations of what im trying to achieve but havent seen a exact example...what im trying to do is put a where clause in my query depending on if a set variable has a value

CASE WHEN (@category_id IS NOT NULL)
THEN
LEFT JOIN sub_categories sc ON sc.sub_category_id = i.sub_category_id
JOIN categories c ON c.category_id = sc.category_id AND c.category_id = :category_id
END

is this possible or have i just got the syntax wrong somewhere...thank you guys

Link to comment
https://forums.phpfreaks.com/topic/292112-case-in-where-statement/
Share on other sites

Can't do a conditional JOIN like that.

 

Join the tables regardless. Keep in mind that making categories an inner join that a matching row will be required, even if you're JOINing it against a table which is an outer join, so it should probably be a LEFT JOIN as well.

 

If that doesn't do it, what problem are you trying to fix or what are you trying to accomplish?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.