需要使用嵌套选择的Join创建更新

时间:2014-11-11 01:38:52

标签: mysql sql

我有这个:

SELECT users.first_name,
       users.last_name,
       family_products.costs_obj
FROM   users
       JOIN family_products
         ON users.family_id = family_products.family_id
WHERE  users.family_id IN (SELECT family_id
                           FROM   employer_families
                           WHERE  employer_id = 117)
       AND family_products.product_id IN (SELECT id
                                          FROM   market_products
                                          WHERE  type = "medicalplan")
       AND users.first_name = 'alexandre' 

我需要能够将cost_obj更新为=''我如何将此选择作为更新运行?

2 个答案:

答案 0 :(得分:0)

虽然我无法测试这个,但我认为这就是你所需要的:

UPDATE fp
  SET fp.costs_obj = ''
FROM
  users u
  JOIN family_products fp ON u.family_id = fp.family_id
WHERE
  u.family_id IN 
  (
    SELECT
      family_id
    FROM
      employer_families
    WHERE
      employer_id = 117
  ) 
  AND
  fp.product_id IN 
  (
    SELECT
      id
    FROM
      market_products
    WHERE
      type = "medicalplan"
  ) 
  AND
    u.first_name = 'alexandre';

答案 1 :(得分:0)

没有测试过,因为我不知道架构,但你可以试试这个:

UPDATE family_products
SET costs_obj = ''
WHERE costs_obj IN(
    SELECT 
       family_products.costs_obj
    FROM   users
       JOIN family_products
       ON users.family_id = family_products.family_id
    WHERE  users.family_id IN (SELECT family_id
                           FROM   employer_families
                           WHERE  employer_id = 117)
       AND family_products.product_id IN (SELECT id
                                          FROM   market_products
                                          WHERE  type = "medicalplan")
       AND users.first_name = 'alexandre' 
)