【问题标题】:Update with Select on QueryDSL is possible? How?可以在 QueryDSL 上使用 Select 进行更新吗?如何?
【发布时间】:2020-10-02 09:45:57
【问题描述】:

我需要在 MySQL 上运行类似于下面的更新语句。它根据同一表中行的条件更新表中的值。有没有办法在 QueryDSL JPA(甚至是原生 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;

【问题讨论】:

    标签: java mysql jpa querydsl


    【解决方案1】:

    您可以尝试使用派生表:

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

    我已从您的查询中删除了几个部分,但您了解了解决方案的要点。

    【讨论】:

    • 我发布的查询已经在工作,我问如何通过 QueryDSL JPA 或 SQL 库来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多