Answer the question
In order to leave comments, you need to log in
How to replace where exists with join in my case?
Good day, I ran into a problem, there is a SQL query that works for quite a long time
select
*
from
`brands`
where
exists (
select
*
from
`products`
where
`brands`.`id` = `products`.`brand_id`
and `show` = 1
and exists (
select
*
from
`categories`
where
`products`.`category_id` = `categories`.`id`
and (`id` = 9
or `parent_id` = 9)))
SELECT
*
from
brands
inner join products ON
`brands`.id = `products`.brand_id
left join categories ON
`products`.`category_id` = `categories`.`id`
where
products.`show` = 1 and (categories.id = 9 or categories.parent_id = 9);
Answer the question
In order to leave comments, you need to log in
All by myself doper
SELECT
DISTINCT `brands`.*
from
brands
inner join products ON
`brands`.id = `products`.brand_id
left join categories ON
`products`.`category_id` = `categories`.`id`
where
products.`show` = 1 and (categories.id = 9 or categories.parent_id = 9);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question