MySQL
3
Вклад в тег
SELECT mags.*, COUNT(*) AS c
FROM mag_tov, tovs, mags
WHERE mt_tov_id = tov_id AND mt_mag_id = mag_id
AND tov_name IN ("ПЕРЕЧИСЛИТЕ_ЧТО_ХОТИТЕ")
GROUP BY mag_id
HAVING c = КОЛЛИЧЕСТВО_ТОГО_ЧТО_ХОТИИТЕ
SELECT mags.*, COUNT(*) AS c
FROM mag_tov, tovs, mags
WHERE mt_tov_id = tov_id AND mt_mag_id = mag_id
AND tov_name IN ('молоко', 'мясо' и 'чай')
GROUP BY mag_id
HAVING c = 3
SET @place := 0, @map_id := 0;
SELECT
IF(@map_id != map_id, @place := 1, @place := @place + 1) AS place,
IF(@map_id != map_id, @map_id:=map_id, map_id) AS map_id,
user_id, map_score , level, @place
FROM (
SELECT m.map_id, m.user_id, m.score AS map_score, p.level
FROM MapsScores AS m, PlayersData AS p
WHERE m.user_id = p.id
GROUP BY m.map_id, m.user_id
ORDER BY m.map_id ASC, m.score DESC
) AS tops
GROUP BY map_id, user_id HAVING place <= СКОЛЬКО_НАРОДУ_ХОТИТЕ_В_ТОП
ORDER BY map_id ASC, map_score DESC