0

我有这个查询

SELECT users.name 
FROM users 
LEFT JOIN weapon_stats ON users.id = weapon_stats.zp_id
WHERE weapon_stats.weapon = 26
ORDER BY weapon_stats.kills DESC
LIMIT 1;

问题是你需要得到从 1 到 30 变化的结果,这很奇怪..

是否可以在一个查询中获得所有结果?

我想让每个用户用每种武器杀死最多

武器 1

SELECT users.name 
FROM users 
LEFT JOIN weapon_stats ON users.id = weapon_stats.zp_id
WHERE weapon_stats.weapon = 01
ORDER BY weapon_stats.kills DESC
LIMIT 1;

武器 2

SELECT users.name 
FROM users 
LEFT JOIN weapon_stats ON users.id = weapon_stats.zp_id
WHERE weapon_stats.weapon = 02
ORDER BY weapon_stats.kills DESC
LIMIT 1;

提前致谢!

4

4 回答 4

1
select U.name, W.weapon, W.kills
FROM users U
INNER JOIN weapons W on W.zp_id = U.id
INNER JOIN 
  (SELECT WW.weapon, MAX(WW.kills) AS kills 
    FROM weapons WW 
    WHERE WW.weapon BETWEEN 1 AND 30
    GROUP BY WW.weapon) K
  ON W.kills = K.kills AND W.weapon = K.weapon

我敢肯定它可以通过优化变得更快,但这最终会奏效

jsfiddle链接

于 2013-05-10T20:41:16.993 回答
1
WHERE weapon_stats.weapon BETWEEN 1 AND 30
于 2013-05-10T20:42:24.810 回答
0

我不确定“将位置从 1 更改为 30”是什么意思,但请参阅以下内容:

[..]WHERE wearpon_stats.weapon 1 到 30 之间;

更多信息:https ://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between

于 2013-05-10T20:42:47.713 回答
0

除非我误解了您要查找的内容,否则您不能执行以下操作吗?

SELECT users.name 
FROM users 
LEFT JOIN weapon_stats ON users.id = weapon_stats.zp_id
WHERE weapon_stats.weapon BETWEEN 1 and 30
ORDER BY weapon_stats.kills DESC
LIMIT 1;
于 2013-05-10T20:43:27.123 回答