如何区分SQLite语句中的第一行

时间:2013-06-07 12:57:07

标签: sqlite android-sqlite

我有这样的表:

_id| letter| extra
1  | A     | some
2  | A     | another
3  | A     | information
4  | B     | NULL
5  | B     | 98090

当LETTER改变时,我需要区分第一行:

_id| letter| extra       | mediator
1  | A     | some        | 1
2  | A     | another     | 
3  | A     | information |
4  | B     | NULL        | 1
5  | B     | 98090       |

或......

_id| letter| extra       | count
1  | A     | some        | 1
2  | A     | another     | 2
3  | A     | information | 3
4  | B     | NULL        | 1
5  | B     | 98090       | 2

(在这种情况下,我知道我需要数= 1)

1 个答案:

答案 0 :(得分:1)

以下查询搜索这些ID(假设“first”表示“最小_id值”):

SELECT MIN(_id)
FROM MyTable
GROUP BY letter

以下语句将设置mediator列:

UPDATE MyTable
SET mediator = 1
WHERE _id IN (SELECT MIN(_id)
              FROM MyTable
              GROUP BY letter)