I have two tables:
score_table
with columns player_id
and score
, where player_id is unique
group_table
with columns player_id
and group_id
, where player_id is unique.
There is ~1000 entries in both tables, and two different values in 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 |
| ... ... | | ... ... |
| ... ... | | ... ... |
I would like to select the player with the smallest score, but it must be in group_id 0
Base on this answer, the query which I've come up with:
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
)"
However, the query is constructed incorrectly, and the result is always null
Edit:
simple " SELECT MAX(st.score) as score FROM score_table pg"
does return 44, which is the correct, highest score. I've tested all the entries using similar queries and each one is retrievable.
Both tables are using BTREE for player_id, which is unique in both cases.
Edit:
"SELECT * FROM score_table as st JOIN group_table gt ON pg.player_id = gt.player_id"
followed by var_dump
outputs an arary of N-entries, where each entry contains columns from both tables, joined together, correctly
Answer Accepted
So the issue was in the alias of table names.
When we SELECT MIN(st.score)
the result will not be accessible with fetch(PDO::FETCH_NUMERIC)['score']
I had to use ['MIN(st.score)']
as the key.