在 SQLite3 中,是否可以列出特定的行?
In SQLite3, is it possible to list out specific rows?
假设我有一个 table:
+--------------+--------------+------+-----+
| ID | Score | email | add |
+--------------+--------------+------+-----+
| 123 | 88 | 123@gmail.com | somewhere |
| 456 | 77 | 123@gmail.com | somewhere |
| 789 | 88 | 123@gmail.com | somewhere |
| 111 | 77 |123@gmail.com | somewhere |
| 555 | 77 | 123@gmail.com | somewhere |
|444 | 88 | 123@gmail.com | somewhere
| 222 | 77 | 123@gmail.com | somewhere |
| 333 | 88 |123@gmail.com | somewhere |
我的问题是 select Score
列是否可以只打印前 3 88
和 77
分数?
我试过了,好像只给我3个88分
SELECT Score
FROM Table_Name
WHERE Score = '88' OR Score = '77'
LIMIT 3
首先过滤 table 以便只返回具有您想要的分数的行,然后使用 ROW_NUMBER()
window 函数对每个 [=13= 的行进行排名] 基于ID
这样就可以过滤掉超过每组前3行的行:
SELECT ID, Score, email, add
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Score ORDER BY ID) rn
FROM Table_Name
WHERE Score = '88' OR Score = '77'
)
WHERE rn <= 3;
对于 3.25.0 之前不支持 window 函数的 SQLite 版本,请使用相关子查询:
SELECT t1.*
FROM Table_Name t1
WHERE t1.Score = '88' OR t1.Score = '77'
AND (SELECT COUNT(*) FROM Table_Name t2 WHERE t2.Score = t1.Score AND t2.ID <= t1.ID) <= 3;
假设我有一个 table:
+--------------+--------------+------+-----+
| ID | Score | email | add |
+--------------+--------------+------+-----+
| 123 | 88 | 123@gmail.com | somewhere |
| 456 | 77 | 123@gmail.com | somewhere |
| 789 | 88 | 123@gmail.com | somewhere |
| 111 | 77 |123@gmail.com | somewhere |
| 555 | 77 | 123@gmail.com | somewhere |
|444 | 88 | 123@gmail.com | somewhere
| 222 | 77 | 123@gmail.com | somewhere |
| 333 | 88 |123@gmail.com | somewhere |
我的问题是 select Score
列是否可以只打印前 3 88
和 77
分数?
我试过了,好像只给我3个88分
SELECT Score
FROM Table_Name
WHERE Score = '88' OR Score = '77'
LIMIT 3
首先过滤 table 以便只返回具有您想要的分数的行,然后使用 ROW_NUMBER()
window 函数对每个 [=13= 的行进行排名] 基于ID
这样就可以过滤掉超过每组前3行的行:
SELECT ID, Score, email, add
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Score ORDER BY ID) rn
FROM Table_Name
WHERE Score = '88' OR Score = '77'
)
WHERE rn <= 3;
对于 3.25.0 之前不支持 window 函数的 SQLite 版本,请使用相关子查询:
SELECT t1.*
FROM Table_Name t1
WHERE t1.Score = '88' OR t1.Score = '77'
AND (SELECT COUNT(*) FROM Table_Name t2 WHERE t2.Score = t1.Score AND t2.ID <= t1.ID) <= 3;