我有一张桌子(games
),就像这样:
ID| title |category|rank
________________________
11|FIFA 17| ps3 | 3
22|FIFA 17| ps4 | 2
33|FIFA 17|xbox one| 4
44| GTA 5 | ps3 | 3
55| GTA 5 | ps4 | 2
66| GTA 5 |xbox one| 4
77|FIFA 16| ps3 | 3
88|FIFA 16| ps4 | 2
99|FIFA 16|xbox one| 4
我希望title
包含FIFA
的所有行,但不重复!并按rank
ASC
排序,所以:
ID| title |category|rank
________________________
22|FIFA 17| ps4 | 2
88|FIFA 16| ps4 | 2
我尝试了什么:
SELECT * FROM games WHERE title LIKE '%FIFA%' GROUP BY title ORDER BY rank ASC
并且title
中的重复项已被删除,但ORDER BY rank ASC
完全被忽略,结果如下:
ID| title |category|rank
________________________
11|FIFA 17| ps3 | 3
99|FIFA 16|xbox one| 4
修改
我想要所有标题只有在排名最低的地方!
答案 0 :(得分:1)
我认为您只想查看每个标题rank
最低的行...这是一种方式:
SELECT b.Id, b.title, b.category, b.rank
FROM (SELECT title, MIN(rank) as LowestRank
FROM games
GROUP BY title) A
JOIN games B on B.Title = A.Title
and A.LowestRank = B.rank
WHERE B.title LIKE 'FIFA%'
答案 1 :(得分:0)
我可能会建议:
SELECT g.*
FROM games g
WHERE g.title LIKE '%FIFA%' AND
g.rank = (SELECT MIN(g2.rank) FROM games g2 WHERE g2.id = g.id);
答案 2 :(得分:0)
根据您的输出。它看起来你只想要PS4而不是PS3。正确? 试试这个:
SELECT * FROM games WHERE title LIKE '%FIFA%'
and category = 'ps4'
GROUP BY title ORDER BY rank ASC