在一个查询中获取所有WHERE案例

时间:2013-05-10 20:39:48

标签: mysql where

我有这个查询

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 个答案:

答案 0 :(得分: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 link

答案 1 :(得分:1)

WHERE weapon_stats.weapon BETWEEN 1 AND 30

答案 2 :(得分:0)

我不确定你的意思是'改变从1到30的位置',但请参阅以下内容:

[..] WHERE wearpon_stats.weapon BETWEEN 1和30;

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

答案 3 :(得分: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;