如何将特定的SELECT命令与UPDATE结合使用-SQL

时间:2019-04-03 06:45:37

标签: mysql sql database command

我需要更新SQL数据库中某些特定表的列。

我有一个SELECT命令,该命令选择所有正确的表,但我不知道如何将其与UPDATE结合使用(我是一个完整的菜鸟)。

SELECT
  c.id_customer AS id_customer,
  id_gender,
  firstname,
  lastname,
  c.email AS email,
  birthday,
  date_add,
  c.active AS active,
  c.*,
  a.id_group
FROM prstshp_customer_group a
LEFT JOIN prstshp_customer c
  ON (a.id_customer = c.id_customer)
WHERE 1
AND a.id_group = 4
AND c.deleted != 1
AND c.id_shop IN (1)
ORDER BY id_group ASC

我必须用在表id_default_group表中选择的特定条目的值“ 4”来更新表prstshp_customer中名为prstshp_customer_group的列。

1 个答案:

答案 0 :(得分:1)

使用update join

update prstshp_customer
join
(
SELECT
  c.id_customer AS id_customer,
  id_gender,
  firstname,
  lastname,
  c.email AS email,
  birthday,
  date_add,
  c.active AS active,
  a.id_group
FROM prstshp_customer_group a
LEFT JOIN prstshp_customer c
  ON (a.id_customer = c.id_customer)
WHERE 1
AND a.id_group = 4
AND c.deleted != 1
AND c.id_shop IN (1)
)B on prstshp_customer.id_customer=B.id_customer
SET id_default_group=4