I can't understand how this query works. Is count statement join tables? Is this more efficient than normal join?
SELECT
*
FROM
`products`
WHERE
(
SELECT
count(*)
FROM
`categories`
WHERE
`products`.`category_id` = `categories`.`id`
AND `slug` = ?
) >= 1
AND `slug` <> ?
The query you posted is a nested select also known as a subquery. So really there are two queries at work here, an outer query and an inner query. Basically, the (inner) SELECT statement evaluates to a boolean value -- this is the reason for the count and the >= 1 condition. Since the inner query results in a true/false value, the outer where clause sees it as just another condition. Hope that helps!