我有一个名为“table_name”的表,大概是这样的
+----+-----------+
| ID | title |
| 1 | title 1 |
| 2 | title 2 |
| ...| ......... |
| ...| ......... |
| n | title n |
+----+-----------+.
我需要一个返回此结果的查询
+------+-----------+
| n+1 | title 1 |
| n+1 | title 2 |
| ... | ......... |
| ... | ......... |
| n+1 | title n |
+------+-----------+
(n + 1是从table_name中选择最大值(ID))
我该怎么做?
答案 0 :(得分:2)
只需在选择列表中添加子查询:
select (select max(ID) + 1 from tablename), title
from tablename
答案 1 :(得分:1)
窗口功能
SELECT MAX(ID) OVER() + 1, title
FROM table_name
答案 2 :(得分:1)
您可以为变量选择此最大ID,然后只需在您的选择中使用它:
DECLARE @maxId INT = (SELECT MAX(ID) FROM [dbo].[table1]);
SELECT @maxId, t.title, t.column2, t.column3 ... from [dbo].[table2] t
答案 3 :(得分:0)
对于Mysql
select (1+max(ID)) as ID ,title
from table_name;