doushi1510 2013-03-18 23:09
浏览 10
已采纳

可排序的项目MySQL结构

I have a list of items in a file table, which way is the most practical to sort records based on how the user has positioned them? There could be possibly hundreds of records per user. Perhaps there is a better way to do this and I've boxed my thinking in?

Option 1: I have a table with the order included. I can easily sort by the order column, however when I add a new field or re-order them on the front-end, I may have to update MANY records, *eg: If there are 50 files, changing file_id 1 to position 50, could renumber the order of every row in turn requiring 50 updates..*

table: file
file_id | name  | order
------------------------
1       | test  | 2
2       | other | 1

Option 2: Organize the file table based off an order table. This requires much more work and error checking, and it's a bit more difficult. Yet I would be updating one row with CSV values and transforming them into Arrays to sort the file column.

table: file
file_id | name 
----------------------
1       | test
2       | other

table: order
file_order 
----------------------
2,1
  • 写回答

1条回答 默认 最新

  • dongwei5740 2013-03-18 23:33
    关注

    for option 1 you could use these SQL requests :

    i added a field user_id to your file table

    to make the 50th file as 3rd position:

    UPDATE table SET order=3 WHERE id=50 AND user_id = xxx
    
    UPDATE table SET order=order+1 WHERE id>3 AND id<50 AND user_id = xxx
    

    to make the 3rd as 50th position:

    UPDATE table SET order=50 WHERE id=3 AND user_id = xxx
    
    UPDATE table SET order=order-1 WHERE id>3 AND id<50 AND user_id = xxx
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)