简体   繁体   English

根据同一表中其他行的值更新行

[英]Update rows based on value of other rows in same table

files_imported
--------
ID (PK)
SPN
FILE_ID
LISTPRICE
ACTION

I need to search and UPDATE TableA and SET ACTION='A' WHERE the value of PRICE in a row with a given SPN is not equal to the value of PRICE found for a different FILE_ID and matching SPN. 我需要搜索并更新TableA并在包含给定SPN的行中PRICE的值不等于为其他FILE_ID和匹配的SPN找到的PRICE的值的情况下SET ACTION ='A'。

I can do it with PHP by breaking the task into smaller queries but i'd rather have a clean solution with MySQL. 我可以通过将任务分解为较小的查询来使用PHP,但我希望使用MySQL提供一个干净的解决方案。

This is how far I've gotten: 这是我走了多远:

UPDATE files_imported fi1
JOIN files_imported fi2
    ON fi1.SPN = fi2.SPN
SET ACTION = 'M' 
WHERE fi1.file_id = 980987987
AND fi1.listprice <> fi2.listprice
UPDATE files_imported fi1, files_imported fi2
SET f11.ACTION = 'M' 
WHERE fi1.file_id = 980987987
AND fi1.SPN = fi2.SPN /* same SPN*/
AND fi1.listprice <> fi2.listprice /* different price */
AND fi1.file_id <> fi2.file_id /* different file */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM