简体   繁体   English

可以在 QueryDSL 上使用 Select 进行更新吗? 如何?

[英]Update with Select on QueryDSL is possible? How?

I need to run an update statement similar to the one below on MySQL.我需要在 MySQL 上运行类似于下面的更新语句。 It updates values in a table based on conditions of rows in the same table.它根据同一表中行的条件更新表中的值。 Is there a way to achieve that in QueryDSL JPA (or even that native one)?有没有办法在 QueryDSL JPA(甚至是原生的)中实现这一点?

UPDATE items,
       (SELECT id, retail / wholesale AS markup, quantity FROM items)
       AS discounted
    SET items.retail = items.retail * 0.9
    WHERE discounted.markup >= 1.3
    AND discounted.quantity < 100
    AND items.id = discounted.id;

You can try using derived table:您可以尝试使用派生表:

UPDATE items
SET items.retail = items.retail * 0.9
WHERE
items.id in (SELECT id FROM (select * from items) discounted WHERE discounted.quantity < 100);

I've removed few parts from your query but you get gist of the solution.我已从您的查询中删除了一些部分,但您已了解解决方案的要点。

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

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