php - 一次更新两个 ID SQL - PHP

标签 php sql

我有一个表,它的 PK 是一个 int 字段。 我的问题是我需要交换其中两个值。

这是我当前的代码:

$newId = (int)$id; $newId = $newId - 1;

$result = mysql_query("UPDATE homeScroller SET id = '$newId' WHERE id = '$id'") or die('error '.mysql_error()); 
$result2 = mysql_query("UPDATE homeScroller SET id = '$id ' WHERE id = '$newId'") or die('error '.mysql_error());

该表最多包含 3 行,其中包含网站首页上幻灯片的信息。首页按 ID 对它们进行排序。因此,要更改顺序,我需要编辑 ID。

最佳答案

The table contains at max 3 rows that contain info for an slideshow on the frontpage of the website. The frontpage orders them by their IDs. So to change the order I need to edit the ID's.

如果是这种情况,那么您的应用程序/数据库的架构设计很差,您需要在继续之前重新考虑您的数据库设计。

主键应该是不可变的,即:它们应该永远改变。你所说的就是改变主键。

修复您的应用程序/数据库,您就不会遇到此问题。您确定不应该只添加新字段“sort_order”并使用 ORDER BY sort_order 或按 ID 字段排序吗?

关于php - 一次更新两个 ID SQL - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4491697/

相关文章:

php - 按两个不同的日期和时间mysql分组

PHP UPDATE 充当 INSERT

sql - 索引::1 处缺少 IN 或 OUT 参数

MySQL 从 WooCommerce 订单获取 SKU

javascript - 在输出中使用 AJAX/多个 sql 语句显示外键描述而不是值

php - 在 php json_decode 中查找错误行

php - 通过 PHP 修改 XML 并输出 xml(几乎就像过滤器 feed)

php - Magento 主题 CSS 未加载

php - 如何在脚本运行时逐步回显对用户的响应?

sql - 在选择查询中插入 if