SELECT * FROM (
(SELECT *, 0 as `order`
FROM `table`
WHERE `smen` <= 0 AND `cash` > 500
ORDER BY `cash` DESC
LIMIT 1)
UNION
(SELECT *, 1
FROM `table`
WHERE `smen` <= 1 AND `cash` > 500
ORDER BY `cash` DESC
LIMIT 1)
UNION
(SELECT *, 2
FROM `table`
WHERE `smen` <= 0
ORDER BY `cash` DESC
LIMIT 1)
UNION
(SELECT *, 3
FROM `table`
WHERE `smen` <= 1
ORDER BY `cash` DESC
LIMIT 1)
)
ORDER BY `order`
LIMIT 1
SELECT ...
FROM `items`
WHERE `id` IN (
(SELECT `itemId`
FROM `tags_items`
WHERE `tagId` IN (SELECT `id` FROM `tags` WHERE `tag` = :word))
UNION
(SELECT `itemId`
FROM `styles_items`
WHERE `styleId` IN (SELECT `id` FROM `styles` WHERE `style` = :word))
UNION
(SELECT `itemId`
FROM `labels_items`
WHERE `labelId` IN (SELECT `id` FROM `labels` WHERE `label` = :word))
)
SELECT `c`.`id_category`, `c`.`name`, IFNULL(`p`.`count`, 0) AS `count`
FROM `category` AS `c`
LEFT JOIN (
SELECT `id_category`, COUNT(*) AS `count`
FROM `product`
GROUP BY `id_category`
) AS `p` ON `p`.`id_category` = `c`.`id_category`
либоSELECT `c`.`id_category`, `c`.`name`, IFNULL(`p`.`count`, 0) AS `count`
FROM (
SELECT `id_category`, COUNT(*) AS `count`
FROM `product`
GROUP BY `id_category`
) AS `p`
RIGHT JOIN `category` AS `c` ON `c`.`id_category` = `p`.`id_category`