select `id`, `title`, `slug`, `label`, `price`, `old_price`, `category_id`, `picture_id`, `brand_id` from `products` where (exists (select * from `brands` where `products`.`brand_id` = `brands`.`id` and (`title` like '%лампа%' or `tags` like '%лампа%')) or `title` like '%лампа%') group by `products`.`id` order by `products`.`sales` desc, `products`.`views` desc limit 18
select `id`, `title`, `slug`, `label`, `price`, `old_price`, `category_id`, `picture_id`, `brand_id` from `products` where (exists (select * from `brands` where `products`.`brand_id` = `brands`.`id` and (`title` like '%лампа%' or `tags` like '%лампа%')) or `title` like '%лампа%') group by `products`.`id` order by `products`.`sales` desc, `products`.`views` desc limit 18