SELECT DISTINCT `a`.`attribute_id`, `a`.`text`
FROM (
SELECT DISTINCT `p`.`id`
FROM `product` AS `p`
JOIN `product_category` AS `c` ON `c`.`product_id` = `p`.`id`
AND `c`.`category_id` = :categoryId
JOIN `product_attribute` AS `a1` ON `a1`.`product_id` = `p`.`id`
AND `a1`.`attribute_id` = :attribute1 AND `a1`.`text` = :attrValue1
JOIN `product_attribute` AS `a2` ON `a2`.`product_id` = `p`.`id`
AND `a2`.`attribute_id` = :attribute2 AND `a2`.`text` = :attrValue2
...
JOIN `product_attribute` AS `aN` ON `aN`.`product_id` = `p`.`id`
AND `aN`.`attribute_id` = :attributeN AND `aN`.`text` = :attrValueN
) AS `pr`
JOIN `product_attribute` AS `a` ON `a`.`product_id` = `p`.`id`
WHERE `a`.`attribute_id` NOT IN (:attribute1, :attribute2,... attributeN)
ORDER BY `a`.`attribute_id, `a`.`text`
SELECT `u1`.`name`, `u2`.`name`, COUNT(*) AS `count`
FROM `users_groups` AS `ug1`
JOIN `users_groups` AS `ug2`
ON `ug2`.`group_id` = `ug1`.`group_id`
AND `ug2`.`user_id` > `ug1`.`user_id`
JOIN `users` AS `u1`
ON `u1`.`id` = `ug1`.`user_id`
JOIN `users` AS `u2`
ON `u2`.`id` = `ug2`.`user_id`
GROUP BY `ug1`.`user_id`, `ug2`.`user_id`
ORDER BY `count` DESC
LIMIT 1
SELECT *
FROM (
SELECT `iPageID`
FROM `page_city`
WHERE `iCityID` IN (101, 102)
GROUP BY `iPageID`
HAVING COUNT(*) = 2
) AS `t1`
JOIN (
SELECT `iPageID`
FROM `page_city`
GROUP BY `iPageID`
HAVING COUNT(*) = 2
) AS `t2` USING (`iPageID`)