使用内部联接更新查询的正确语法

Correct syntax for update query with inner join

我正在尝试更新所有与查询匹配的产品中的字段 buyPrice,正确的语法是什么?

我遇到了这个错误

An error occurred while running the statement: sub-select returns 22 columns - expected 1 
(code 1 SQLITE_ERROR): , while compiling: UPDATE product set buyPrice = ? in 
(SELECT * FROM product p JOIN roomuser u ON p.businessId = u.currentBusinessId WHERE sellPrice 10

到目前为止,这是我的查询:

@Query("""
    UPDATE product 
        set buyPrice = :newPrice in 
        (SELECT * FROM 
        product p JOIN user u ON p.businessId = u.currentBusinessId
        WHERE sellPrice < 10)
    """)

suspend fun updateProduct(newPrice: Float)

我认为您想在 UPDATE 语句中使用 WHERE 子句,它可能是这样的:

UPDATE product 
SET buyPrice = :newPrice 
WHERE product_id IN (
  SELECT p.product_id 
  FROM product p JOIN user u 
  ON p.businessId = u.currentBusinessId
  WHERE sellPrice < 10
)

我在我的代码中使用 product_id,我认为它是 table product 的主键。

或者,如果 sellPrice 是 table product 的列,您可以使用 EXISTS 而不是这样的连接:

UPDATE product 
SET buyPrice = :newPrice 
WHERE sellPrice < 10
  AND EXISTS (SELECT 1 FROM user u WHERE u.currentBusinessId = product.businessId)