douyou7072 2014-02-19 16: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 16: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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配