0

我有一个用于创建视图的 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
4

2 回答 2

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
于 2013-01-15T18:38:14.247 回答
0

您可以添加一个触发器,也许可以使用它来解决。 http://dev.mysql.com/doc/refman/5.0/es/triggers.html

于 2013-01-15T18:50:56.210 回答