sqlite 将自动增量列添加到查询结果

p5cysglq  于 2022-11-24  发布在  SQLite
关注(0)|答案(2)|浏览(344)

如果不创建包含自动增量列的TEMP表,如何向查询结果中添加自动增量字段?例如,

SELECT ???, * FROM MyTable

应导致:

1|*
2|*
3|*
k5ifujac

k5ifujac1#

我认为您正在查找行号。请尝试以下内容:

SELECT (SELECT COUNT(0) 
        FROM supportContacts t1 
        WHERE t1.id <= t2.id 
        ) AS 'Row Number', * FROM supportContacts t2 ORDER BY id;

示例表:

╔════╦═════════╦═════════════════════╗
║ id ║  type   ║       details       ║
╠════╬═════════╬═════════════════════╣
║  1 ║ Email   ║ admin@sqlfiddle.com ║
║  2 ║ Twitter ║ @sqlfiddle          ║
╚════╩═════════╩═════════════════════╝

结果:

╔════════════╦════╦═════════╦═════════════════════╗
║ Row Number ║ id ║  type   ║       details       ║
╠════════════╬════╬═════════╬═════════════════════╣
║          1 ║  1 ║ Email   ║ admin@sqlfiddle.com ║
║          2 ║  2 ║ Twitter ║ @sqlfiddle          ║
╚════════════╩════╩═════════╩═════════════════════╝

查看此SQLFiddle

9avjhtql

9avjhtql2#

我想说的是,Himanshu的答案对我来说是100%有效的。我不是OP,但是当我需要一个解决方案的时候,我发现了这个线程。下面是我使用更通用的表名的代码。

SELECT (SELECT COUNT(0)  
    FROM ENTRIES t1  
    WHERE t1.Created_Date<= t2.Created_Date  
    ) AS 'id', * FROM ENTRIES t2  
    WHERE Team = 'Sales' 
    ORDER BY id

相关问题