从MySQL结果自动更新列

时间:2013-01-15 18:27:57

标签: php mysql

我有一个用于创建视图的SQL查询。是否有可能在该查询运行时,它将更新表中的相应字段并分配用户?

例如: 我的查询,会给我这个观点:

   dealID |  removalnotes  | rolloverenabled   | rateplanchanges
   1      |  Yes           | Yes               | NULL
   2      | Null           | Null              | NULL
   3      | Null           | Yes               | NULL
   4      | Null           | Yes               | Yes

这是我的问题:

SELECT dealID, removalnotes, rolloverenabled , rateplanchanged
FROM invoice_payment
WHERE removalnotes IS NULL OR removalnotes <> 'Yes' 
OR rolloverenabled IS NULL OR rolloverenabled <> 'Yes' 
OR rateplanchanged IS NULL OR rateplanchanged <> 'Yes'
GROUP BY dealID

我可以在那里添加一个自动执行此操作的子查询吗?如果可以,我在哪里放置或如何放置子查询?

UPDATE invoice_payment SET
user = 'User1'
WHERE dealID = dealID

1 个答案:

答案 0 :(得分:1)

试试这个,

           UPDATE 
              invoice_payment AS ip
            CROSS JOIN (
              SELECT dealID, removalnotes, rolloverenabled , rateplanchanged
            FROM invoice_payment
            WHERE removalnotes IS NULL OR removalnotes <> 'Yes' 
            OR rolloverenabled IS NULL OR rolloverenabled <> 'Yes' 
            OR rateplanchanged IS NULL OR rateplanchanged <> 'Yes'
            GROUP BY dealID

            ) AS sq
            SET 
              ip.user = 'User1' 
            WHERE ip.dealID = dealID