您的查询包含错误:未定义别名“ja”。
试试这样:
SELECT
( SELECT COUNT(*) + 1
FROM "table"
WHERE title < t.title OR (title = t.title AND id<t.id)
) as rowIndex,
t.title
FROM "table" t
ORDER BY t.title;
但是,请注意,此子查询构造不会很好地扩展。对于大型数据集,您可能希望创建一个临时表并改用 ROWID(如讨论的,例如, here)。
编辑:
使用 COLLATE NOCASE 进行测试:
CREATE TABLE "table" (id INTEGER, title TEXT COLLATE NOCASE);
INSERT INTO "table" VALUES
(1, "Book A"),
(2, "Book b"),
(3, "Book C"),
(4, "Book B"),
(5, "Book a");
查询产生:
1|Book A
2|Book a
3|Book b
4|Book B
5|Book C
编辑:
如果您不想声明列 COLLATE NOCASE,则必须确保在 ORDER BY 部分和子查询中使用 COLLATE:
SELECT
( SELECT COUNT(*) + 1
FROM "table"
WHERE title < t.title COLLATE NOCASE OR (title = t.title COLLATE NOCASE AND id<t.id)
) as rowIndex,
t.title
FROM "table" t
ORDER BY t.title COLLATE NOCASE;