更新行序列
我有一个表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这就是您正在寻找的代码。
http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with- Different-values-and-a-single-sql-query/
I think this is the code you are looking for.
http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/