Mysql查询获取具有一个条件的所有值和具有相同ID的其他值

时间:2016-10-05 10:15:55

标签: mysql

我有这张桌子。我需要获得所有预测值> GAME MIN_VALUE 具有相同ID_GAME的所有PREDICTION值,其中至少有一个预测值> GAME MIN_VALUE。

在这个例子中:

GAME

ID     PLAYER     MIN_VALUE
1      John       1.2
2      Paul       1.4
3      Henry      1.6
4      Tom        2.1

PREDICTIONS

ID_GAME     HOUSE       VALUE    
1           house_a     1.5
1           house_b     1
2           house_a     1.2
2           house_b     1.4
2           house_c     1.5
3           house_a     1.5
3           house_b     1.7
3           house_c     1.8
3           house_d     1.9
4           house_b     1.6
4           house_d     1.9

我需要得到这个:

ID_GAME     HOUSE       PLAYER     VALUE   MIN_VALUE 
1           house_a     John       1.5     1.2
1           house_b     John       1       1.2
2           house_a     Paul       1.2     1.4
2           house_b     Paul       1.4     1.4
2           house_c     Paul       1.5     1.4
3           house_a     Paul       1.5     1.4
3           house_b     Henry      1.7     1.6
3           house_c     Henry      1.8     1.6
3           house_d     Henry      1.9     1.6

我尝试过这个mysql查询没有很好的结果:

SELECT *                  
FROM game m
INNER JOIN predictions p1 ON m.id = p1.id_game
INNER JOIN predictions p2 ON m.id = p2.id_game
WHERE p1.value >= m.min_value OR p2.value < m.min_value
AND p1.id_id_game = p2.id_id_game

1 个答案:

答案 0 :(得分:0)

您可以尝试放置&#39; 反引号&#39;围绕表名MATCH并使用此查询:

SELECT ID_MATCH, HOUSE, PLAYER,  VALUE ,  MIN_VALUE 
FROM PREDICT
LEFT JOIN  `MATCH` ON  `MATCH`.ID = PREDICT.ID_MATCH

MATCH是MySQL中的保留关键字。欲了解更多详情,请访 https://dev.mysql.com/doc/refman/5.7/en/keywords.html#idm140118802075536