自动增加字段,在另一个字段中的更改后重置 [英] Auto-increment field that resets after a change in another field
问题描述
您可以提供一个非常简单的SQL示例来说明如何创建可自动递增的计数或顺序字段,但在不同字段中的每次更改后重新启动?在下表中,每次膳食字段发生更改时,订单字段将重新启动为1。
Can you provide a very simple SQL example of how to create a "count" or "order" field that would auto-increment, but restart after every change in a different field? In the table below, the "Order" field would restart at "1" every time there was a change in the "Meal" field. Thanks.
膳食 时间 订购
午餐 10:30 1Æ
午餐 11:00
午餐 11:15 3⑶
晚餐4:30 ; 1
晚餐 4:45 2载
晚餐 ; 5:00 3Æ
晚餐5:30 br />
Meal Time Order
Lunch 10:30 1
Lunch 11:00 2
Lunch 11:15 3
Dinner 4:30 1
Dinner 4:45 2
Dinner 5:00 3
Dinner 5:30 4
推荐答案
不要在表格中存储 Order
将其添加到视图。
Instead of storing Order
in the table, consider adding it to a view. You can select from the view instead of the table when you need it.
视图可以使用 row_number()
以计算顺序,如:
The view could use row_number()
to calculate the order, like:
select row_number() over (partition by Meal order by Time)
, *
from YourTable
这篇关于自动增加字段,在另一个字段中的更改后重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!