douyou7072 2014-02-19 08:44
浏览 96
已采纳

在数据库中存储项目顺序有哪些方法?

I'm creating a portfolio website that has galleries that contain images. I want the user of this portfolio to be able to order the images within a gallery. The problem itself is fairly simple I'm just struggling with deciding on a solution to implement.

There are 2 solutions I've thought of so far:

  1. Simply adding an order column (or priority?) and then querying with an ORDER BY clause on that column. The disadvantage of this being that to change the order of a single image I'd have to update every single image in the gallery.
  2. The second method would be to add 2 nullable columns next and previous that simply store the ID of the next and previous image. This would then mean there would be less data to update when the order was changed; however, it would be much more complex to set up and I'm not entirely sure how I'd actually implement it.

Extra options would be great.

  • Are those options viable?
  • Are there better options?
  • How could / should they be implemented?

The current structure of the two tables in question is the following:

mysql> desc Gallery;

+--------------+------------------+------+-----+-------------------+-----------------------------+
| Field        | Type             | Null | Key | Default           | Extra                       |
+--------------+------------------+------+-----+-------------------+-----------------------------+
| id           | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| title        | varchar(255)     | NO   |     | NULL              |                             |
| subtitle     | varchar(255)     | NO   |     | NULL              |                             |
| description  | varchar(5000)    | NO   |     | NULL              |                             |
| date         | datetime         | NO   |     | NULL              |                             |
| isActive     | tinyint(1)       | NO   |     | NULL              |                             |
| lastModified | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+--------------+------------------+------+-----+-------------------+-----------------------------+

mysql> desc Image;
+--------------+------------------+------+-----+-------------------+-----------------------------+
| Field        | Type             | Null | Key | Default           | Extra                       |
+--------------+------------------+------+-----+-------------------+-----------------------------+
| id           | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| galleryId    | int(10) unsigned | NO   | MUL | NULL              |                             |
| description  | varchar(250)     | YES  |     | NULL              |                             |
| path         | varchar(250)     | NO   |     | NULL              |                             |
| lastModified | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+--------------+------------------+------+-----+-------------------+-----------------------------+

Currently there is no implementation of ordering in any form.

展开全部

  • 写回答

2条回答 默认 最新

  • doujujian0052 2014-02-19 08:50
    关注

    while 1 is a bit ugly you can do:

      UPDATE table set order=order+1 where order>='orderValueOfItemYouCareAbout';
    

    this will update all the rest of the images and you wont have to do a ton of leg work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部