更新行序列

发布于 2024-11-25 04:46:04 字数 1792 浏览 1 评论 0原文

我有一个表 sites ,基本上是一个旅行推销员问题。我的老板想要从列表中选择一堆站点,然后手动将它们排序为访问顺序。我寻找过类似问题,但它们并不是针对MySQL的,而且那些 were 没有为我的情况提供合理的解决方案。我在大学没有学计算机科学,所以希望这对你们中的一些人来说是重要的东西。

我想做类似以下伪代码的操作:

UPDATE sites SET run_order=0 WHERE selected='false';
UPDATE sites SET run_order=AUTO_SEQUENCE(DESC FROM 6) WHERE site_id=SEQUENCE(23,17,9,44,2,14);

后者具有相同的效果:

UPDATE sites SET run_order=6 WHERE site_id=23;
UPDATE sites SET run_order=5 WHERE site_id=17;
UPDATE sites SET run_order=4 WHERE site_id=9;
UPDATE sites SET run_order=3 WHERE site_id=44;
UPDATE sites SET run_order=2 WHERE site_id=2;
UPDATE sites SET run_order=1 WHERE site_id=14;

由于我通过 PHP 运行此程序,因此我不想发出许多单独的查询,即使站点数量很多我老板一天能拜访的地方当然是受到内燃机的限制。
我的 SQL 表如下所示:

+---------------+----------------------+------+-----+---------+----------------+
| Field         | Type                 | Null | Key | Default | Extra          |
+---------------+----------------------+------+-----+---------+----------------+
| site_id       | int(10) unsigned     | NO   | PRI | NULL    | auto_increment |
| ...           |                      |      |     |         |                |
| selected      | enum('false','true') | NO   |     | false   |                |
| run_order     | int(10) unsigned     | NO   |     | 0       |                |
+---------------+----------------------+------+-----+---------+----------------+

I have a table sites and basically a travelling salesman problem. My boss wants to select a bunch of sites out of the list, then sort them manually into a visit order. I have looked for similar questions, but they were not targeted at MySQL, and those that were didn't provide a reasonable solution for my situation. I didn't do Computer Science at university, so hopefully this is bread-and-butter stuff for some of you out there.

I would like to do something like the following pseudo code:

UPDATE sites SET run_order=0 WHERE selected='false';
UPDATE sites SET run_order=AUTO_SEQUENCE(DESC FROM 6) WHERE site_id=SEQUENCE(23,17,9,44,2,14);

The latter of those would have the same effect as:

UPDATE sites SET run_order=6 WHERE site_id=23;
UPDATE sites SET run_order=5 WHERE site_id=17;
UPDATE sites SET run_order=4 WHERE site_id=9;
UPDATE sites SET run_order=3 WHERE site_id=44;
UPDATE sites SET run_order=2 WHERE site_id=2;
UPDATE sites SET run_order=1 WHERE site_id=14;

Since I am running this via PHP, I don't want to have to issue many individual queries, even though the number of sites my boss could visit in a day is of course limited by the internal combustion engine.
My SQL table looks like this:

+---------------+----------------------+------+-----+---------+----------------+
| Field         | Type                 | Null | Key | Default | Extra          |
+---------------+----------------------+------+-----+---------+----------------+
| site_id       | int(10) unsigned     | NO   | PRI | NULL    | auto_increment |
| ...           |                      |      |     |         |                |
| selected      | enum('false','true') | NO   |     | false   |                |
| run_order     | int(10) unsigned     | NO   |     | 0       |                |
+---------------+----------------------+------+-----+---------+----------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文