Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL中的三角交换?(通过PHP)_Php_Mysql - Fatal编程技术网

SQL中的三角交换?(通过PHP)

SQL中的三角交换?(通过PHP),php,mysql,Php,Mysql,我有一个SQL表,它看起来像: --------------------- | price |price_two | --------------------- | 35.90 | 0 | | 21.90 | 5 | | 7.90 | 0 | | 50.21 | 30.29 | --------------------- 现在我想在SQL中创建一个查询,将所有值从price切换到price\u-two,其中price\u-two低于p

我有一个SQL表,它看起来像:

---------------------
| price  |price_two |
---------------------
| 35.90  |     0    |
| 21.90  |     5    |
| 7.90   |     0    |
| 50.21  |   30.29  |
---------------------
现在我想在SQL中创建一个查询,将所有值从price切换到price\u-two,其中price\u-two低于price但不是0

所以,最后我想说:

---------------------
| price  |price_two |
---------------------
| 35.90  |     0    |
|   5    |   21.90  |
| 7.90   |     0    |
| 30.29  |   50.21  |
---------------------
我可以这样做吗?如果可以,怎么做

你好,谢谢

类似于

更新价格P
内部连接价格P2
P.价格=P2.价格
P2.Price_two=P.Price_two
和P.Price_two

我们只需使用一个自连接并使用第二个表的数据来更新第一个表,因为我们需要具体化值并同时执行两个更新,否则我们将失去原始值的句柄

您应该在最后一行获得30.9 | 50.21…而不是50.21 | 30.29。。。看一看,我删除了我的答案。这比看起来更复杂,而且已经得到了回答:@JeremyHarris的可能重复我想我最喜欢其中一条评论:
x=x+y,y=x-y,x=x-y来自izak;但对于缺乏逻辑批判性思维能力的人来说,这有点让人头疼。
UPDATE Price P
INNER JOIN Price P2
  on P.Price = P2.Price
 AND P2.Price_two = P.Price_two
 AND P.Price_two < P.Price
 AND P.Price_two != 0

 SET P.Price = P2.Price_two,
     P.Price_two = P2.Price;