嘗試此查詢 -
SELECT 'MIN' property, GROUP_CONCAT(p1.name) name, p1.level FROM players p1
JOIN (SELECT MIN(level) level FROM players) p2
ON p1.level = p2.level
UNION
SELECT 'MAX' property, GROUP_CONCAT(p1.name) name, p1.level FROM players p1
JOIN (SELECT MAX(level) level FROM players) p2
ON p1.level = p2.level
查詢最高的「體驗」的價值和任何「身份證」 -
SELECT * FROM
(SELECT 'MIN' property, p1.id, p1.level, p1.name FROM players p1
JOIN (
SELECT
level, MAX(experience) experience
FROM
players,
(SELECT @level:=MIN(level) FROM players) t
WHERE level = @level
) p2
ON p1.experience = p2.experience AND p1.level = p2.level
ORDER BY id LIMIT 1) t_min
UNION
SELECT * FROM
(SELECT 'MAX' property, p1.id, p1.level, p1.name FROM players p1
JOIN (
SELECT
level, MAX(experience) experience
FROM
players,
(SELECT @level:=MAX(level) FROM players) t
WHERE level = @level
) p2
ON p1.experience = p2.experience AND p1.level = p2.level
ORDER BY id LIMIT 1) t_max
使用該數據集作爲一個子查詢的加盟與另一個表 -
SELECT * FROM
(
SELECT * FROM
(SELECT 'MIN' property, p1.id, p1.level, p1.name FROM players p1
JOIN (
SELECT
level, MAX(experience) experience
FROM
players,
(SELECT @level:=MIN(level) FROM players) t
WHERE level = @level
) p2
ON p1.experience = p2.experience AND p1.level = p2.level
ORDER BY id LIMIT 1) t_min
UNION
SELECT * FROM
(SELECT 'MAX' property, p1.id, p1.level, p1.name FROM players p1
JOIN (
SELECT
level, MAX(experience) experience
FROM
players,
(SELECT @level:=MAX(level) FROM players) t
WHERE level = @level
) p2
ON p1.experience = p2.experience AND p1.level = p2.level
ORDER BY id LIMIT 1) t_max
) p
LEFT JOIN guild_member gm
ON gm.id = p.id
MySQL有沒有遺憾的分析功能,所以你必須寫一個漫長而枯燥的查詢。 – Benoit 2012-03-03 12:49:55
@Benoit「UNION」的大型連鎖店很無聊嗎? – 2012-03-03 12:55:52
@Michael::)我應該解釋一下無聊是什麼。對我而言,任何需要高劑量複製粘貼的代碼都有點無聊。在這些聯盟中,只有聚合函數發生變化! – Benoit 2012-03-03 12:59:34