mysql查询以获取具有一个条件的所有值和具有相同id的其他值

6qqygrtg  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(281)

我有这张table。我需要得到所有预测值>游戏最小值和所有预测值与相同的id\u游戏,其中至少有一个预测值>游戏最小值。
在本例中:

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
pkwftd7m

pkwftd7m1#

您可以尝试在“表名匹配”周围放置“backtick”并使用以下查询:

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

相关问题