dongpi2503 2016-04-03 17:39
浏览 59

在MySQL记录中生成范围

I read through other similar questions before, but none seems to have answered my question. Apologies if this is a repeat of something else, but I couldn't seem to find this question answered elsewhere.

I've got a database table that looks more or less like this:

ID | URL | Title | Episode | Sequence | Created_at | Modified_at

These are used to assign specific links to episodes of podcasts, and they're displayed in the order illustrated by the Sequence. I've built a Laravel framework that interacts with the table, and I'm able to change the order of things by manipulating the sequence, but I'm having trouble removing things and maintaining the sequence for the purposes that I want it. Say I have the following four things in the table:

id = 5, sequence = 1
id = 6, sequence = 3
id = 7, sequence = 2
id = 8, sequence = 4

When viewing the site, the page would list them in id order 5, 7, 6, 8. However, if I realize that there's been a problem with one of the links, I would remove it, at which point I might be left with ids 5, 7, 8. The sequence would be 1, 2, 4.

What I'm looking for at the moment is a way of "reseeding" the sequence column on a per-episode basis. It'd be something that basically takes all rows with a specific episode, sorts them in ascending order on sequence, and then "renumbers" them in ascending order. The sequence = 1, 2, 4, 5, 7, 8, 9 would change to 1, 2, 3, 4, 5, 6, 7.

  • 写回答

2条回答 默认 最新

  • duansaoguan7955 2016-04-03 18:15
    关注

    It should be a relatively simple query such as

    select
          YT.*
       from
          YourTable YT
       where
          YT.Episode = 'MO50'
       order by
          YT.Sequence
    

    Now, if your sequences are creating gaps for whatever reason you could always implement a mySql variable to increment regardless of the ACTUAL sequence.. Try..

    select
          @tmpRow := @tmpRow +1 as NewSequence,
          YT.*
       from
          YourTable YT,
          ( select @tmpRow := 0 ) sqlvars
       where
          YT.Episode = 'MO50'
       order by
          YT.Sequence
    

    So, even if your actual sequences for a given show "MO50" are 2, 3, 8, 12, 15. The actual column "NewSequence" would show as 1, 2, 3, 4, 5.

    Now, that said, you would STILL have the original "ID" column as the basis of editing any details and or remove episodes as needed... Does this help you?

    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加