我有一个数据库,其中所有内容都使用start_date
和end_date
我想要做的是动态获取一行的版本号。
因此,获取按 start_date 排序的具有特定 item_id 的所有行,然后给出 astart_date
并将end_date
其隔离为一行。我想要的是找出那一排的位置。
例如数据
id | item_id | start_date
1 | 1 | 01-31-2012
2 | 1 | 02-31-2012
3 | 2 | 02-31-2012
4 | 1 | 03-31-2012
5 | 2 | 05-31-2012
6 | 1 | 04-31-2012
如果使用select id from items where item_id = 2 and start_date = 05-31-2012
会返回5
,所以我想要的版本是2
如果使用select id from items where item_id = 1 and start_date = 03-31-2012
会返回4
,所以我想要的版本是3
我能想到的唯一方法是选择所有带有 item_id 的行,然后遍历每个项目检查 start_date,如果它不是我想要的,将 version_number 增加 1。
有没有办法只用mysql?