mysql中的多个自动增量
我正在使用 php 和 mysql。我有一个表,其中 id 列设置为自动增量作为主键。我正在尝试添加另一个名为 sort_order 的列。插入行时,sort_order 列应自动递增。然后用户将能够更改 sort_order 值。但是mysql不允许多于一列的自动增量?
自动递增 sort_order 值的最佳方法是什么?
根据受欢迎的请求,更多解释。
在管理区域中,用户将有一个类别列表。使用 javascript,用户可以拖动他们想要的类别顺序。然后,脚本会以新顺序发布所有 id 的列表,并以旧顺序发布 sort_order 值。
然后我有一个 php 函数,它用新的 sort_order 值更新 mysql。
除了我手动填写了所有 sort_order 值之外,所有这些都已完成。我希望它能够在用户创建新类别时具有价值。
然后我可以使用 sort_order 在前端正确显示类别顺序。
我已经把一切都做好了。但在开发过程中,我手动填写了 sort_order 的值。
I'm using php and mysql. I have a table with the id column set to auto increment as the primary key. I'm trying to add another column called sort_order. The sort_order column should auto increment when the row is inserted. Then a user will be able to change the sort_order value. But mysql does not allow auto increment for more than one column?
What is the best way to auto increment the sort_order value?
By popular Request, some more explanation.
In administration area, the user will have a list of categories. Using javascript the user can drag the order they want the categories in. The script then posts a list of all the ids in the new order, and the sort_order values in the old order.
I then have a php function which updates mysql with the new sort_order value.
All of this is already done, except I had manually filled out all the sort_order values. I want it to be able to have a value when a user creates a new category.
Then I can use the sort_order to display the category order correctly on the front end.
I have everything done already. But in development I manually filled in the values for the sort_order.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
默认情况下,您可以将排序列保留为 NULL。您唯一需要的是更智能的查询。例如:
You can leave your sort column equal to NULL by default. The only thing you need is a little smarter query. For instance:
您可以使用 触发器 在插入时更新行,或者生成查询时在 PHP 代码中选择适当的值。
但是,您应该考虑是否确实需要创建自动递增的 sort_order 值。在显示代码中,您可以对已给出显式排序顺序的项目进行排序,并将其余未排序的项目放置在列表中的适当位置(底部?)。
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.
也许添加一个时间戳并对其进行排序,这实际上为您的数据模型增加了意义。 “sort_order”列意味着您将表示(视图)逻辑放入数据模型中,这是一件坏事。
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.
您可以将 0 存储在 sort_order 中,然后按
sort_order + id
排序。结果将是相同的,因为sort_order
将精确为 0 而不是id
(id
较少)You may store 0 in sort_order and then sort by
sort_order + id
. Results will be same becausesort_order
will be exact 0 instead ofid
( less onid
)您可以使用触发器,参见 http://dev.mysql。 com/doc/refman/5.1/de/create-trigger.html
You could use a trigger, cf http://dev.mysql.com/doc/refman/5.1/de/create-trigger.html