multiple auto increment in mysql

隐身守侯 提交于 2019-11-27 16:09:52

You can leave your sort column equal to NULL by default. The only thing you need is a little smarter query. For instance:

select * 
from something
order by ifnull(sort, id)

You can use a trigger to update your row upon insert, or choose an appropriate value in your PHP code at the time the query is produced.

However, you should consider whether you really need to create an auto-incrementing sort_order value. In your display code, you can sort items that have been given an explicit sort order, and place the remaining unsorted items at the appropriate place (bottom?) in the list.

Maybe add a timestamp and sort on that, which actually adds meaning to your data model. A "sort_order" column means you're putting presentation (view) logic into your data model, which is a Bad Thing.

You may store 0 in sort_order and then sort by sort_order + id. Results will be same because sort_order will be exact 0 instead of id ( less on id)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!