我在Access 2013中的数据库中有一个表。
Table : city
ID_city city
1 Tetuan
5 Rabat
9 Marrakech
10 Agadir
15 Laayoun
我希望在它们旁边添加Rowid编号:
Rowid ID_city city
1 1 Tetuan
2 5 Rabat
3 9 Marrakech
4 10 Agadir
5 15 Laayoun
答案 0 :(得分:2)
执行此操作的一种方法是在子查询中使用count
函数。不确定它是否能够很好地扩展,并且可能有更好的方法......
select
(select count(*) from city where ID_city <= t1.ID_city) as row_number,
*
from city t1
答案 1 :(得分:0)
最好的方法是使用自我加入......
SELECT
COUNT(*) AS Rowid,
C.ID_City,
C.city
FROM City C
INNER JOIN City C1 ON C.ID_City >= C1.ID_City
GROUP By C.ID_City, C.city
答案 2 :(得分:0)
一点VBA还有很长的路要走......
127.0.0.1
SQL语句:
'Module level variables; values will persist between function calls
'To reset the row number, you have to explicitly call Reset
Dim lastValue As Integer
Public Function RowNumber(x) As Integer
'We need this parameter so Access will call the function on each row, instead of only once
lastValue = lastValue +1
RowNumber = lastValue
End Function
Public Sub Reset()
lastValue = 0
End Sub
答案 3 :(得分:-2)
另一种选择:(http://www.openwinforms.com/row_number_to_sql_select.html)
SELECT ROW_NUMBER()
OVER (ORDER BY ID_city) AS Rowid,
ID_City, city
FROM city