【发布时间】:2016-12-14 23:13:41
【问题描述】:
我有两张桌子:
score_table 包含 player_id 和 score 列,其中 player_id 是唯一的
group_table 包含 player_id 和 group_id 列,其中 player_id 是唯一的。
两个表中有大约 1000 个条目,group_table.group_id 中有两个不同的值
score_table group_table
____________________ ____________________
| player_id score | |player_id group_id|
| | | |
| 0 200 | | 1 0 |
| 1 1 | | 3 1 |
| 4 0 | | 2 0 |
| 3 114 | | 0 0 |
| 2 9 | | 4 1 |
| ... ... | | ... ... |
| ... ... | | ... ... |
我想选择得分最低的玩家,但必须在group_id 0中
Base on this answer,我想出的查询:
SELECT player_id FROM (
SELECT MIN(st.score)
FROM score_table st
INNER JOIN group_table gt
ON gt.player_id = st.player_id
WHERE gt.group_id = 0
ORDER BY st.score ASC
)"
但是查询构造错误,结果总是null
编辑:
simple " SELECT MAX(st.score) as score FROM score_table pg" 确实返回 44,这是正确的最高分。我已经使用类似的查询测试了所有条目,并且每个条目都是可检索的。
两个表都使用 BTREE 作为 player_id,这在两种情况下都是唯一的。
编辑:
"SELECT * FROM score_table as st JOIN group_table gt ON pg.player_id = gt.player_id"
随后var_dump 输出一个包含 N 个条目的数组,其中每个条目包含两个表中正确连接在一起的列
回答已接受
所以问题出在表名的别名上。
当我们SELECT MIN(st.score) 时,fetch(PDO::FETCH_NUMERIC)['score'] 将无法访问结果
我必须使用['MIN(st.score)'] 作为密钥。
【问题讨论】:
-
您的查询选择 group_id = 0 的最小分数。而且我认为您不了解 sql 的基础知识...
-
对不起,我没看懂你写的东西。这个想法是从球员那里返回最高分,但只考虑那些在第 0 组的球员。但不知何故,返回 null :/
-
您使用的是哪个 DBMS?后格雷斯?甲骨文?
-
嘿,使用 phpMyAdmin
标签: sql