我的查询提取了我们系统中contacts
总交易0
的所有contact_status_c = Client
和SELECT
contacts.first_name,
contacts.last_name,
contacts.phone_home,
contacts.phone_mobile,
contacts.phone_work,
contacts.primary_address_street,
contacts.primary_address_city,
contacts.primary_address_state,
contacts.primary_address_postalcode,
contacts.primary_address_country,
opportunities_cstm.stage_c,
contacts_cstm.contact_status_c,
SUM(opportunities_cstm.stage_c = 'Pending'
OR opportunities_cstm.stage_c = 'AccountActive'
OR opportunities_cstm.stage_c = 'Reinstated'
OR opportunities_cstm.stage_c = '%Maturity%'
OR opportunities_cstm.stage_c = 'Flagged'
OR opportunities_cstm.stage_c = 'Stalled') AS deal_count
FROM
opportunities
JOIN opportunities_cstm
ON opportunities.id = opportunities_cstm.id_c
RIGHT JOIN contacts
ON opportunities_cstm.contact_id_c = contacts.id
JOIN contacts_cstm
ON contacts_cstm.id_c = contacts.id
WHERE
contacts.deleted = 0
AND opportunities.deleted = 0
AND contacts_cstm.contact_status_c <> 'Lost Client'
GROUP BY
contacts.id
HAVING deal_count = 0
ORDER BY
contacts.first_name ASC,
contacts.last_name ASC
。查询如下:
contact_status_c
它吸引了175个联系人。现在,我正在尝试更新contacts_cstm
表中的所有Lost Clients
条记录,以反映其中所有175个客户端都列为Client
而不是https://stackoverflow.com/questions/8793914/using-a-having-clause-in-an-update-statement
。这就是问题所在。
最初,我只是尝试使用基本的Update语句来执行此操作,但由于它太宽泛,它更新了1881条记录而不是175条。因此,我总结说我需要包括分组和拥有。使用以下提供的信息:
UPDATE
c_1
FROM
contacts_cstm c_1
JOIN (
SELECT
contacts.first_name,
contacts.last_name,
contacts.phone_home,
contacts.phone_mobile,
contacts.phone_work,
contacts.primary_address_street,
contacts.primary_address_city,
contacts.primary_address_state,
contacts.primary_address_postalcode,
contacts.primary_address_country,
opportunities_cstm.stage_c,
contacts_cstm.contact_status_c,
SUM(opportunities_cstm.stage_c = 'Pending'
OR opportunities_cstm.stage_c = 'AccountActive'
OR opportunities_cstm.stage_c = 'Reinstated'
OR opportunities_cstm.stage_c = '%Maturity%'
OR opportunities_cstm.stage_c = 'Flagged'
OR opportunities_cstm.stage_c = 'Stalled') AS deal_count
FROM
Contacts_Cstm
RIGHT JOIN contacts
ON opportunities_cstm.contact_id_c = contacts.id
JOIN opportunities_cstm
ON opportunities.id = opportunities_cstm.id_c
JOIN opportunities
ON opportunities_cstm.id_c = opportunities.id
WHERE
contacts.deleted = 0
AND opportunities.deleted = 0
AND contacts_cstm.contact_status_c <> 'Lost Client'
GROUP BY
contacts.id
HAVING
deal_count = 0
) contacts_cstm as c_2
ON
c_1.id_c = c_2.id_c
SET
contact_status_c = 'Lost Client'
(这是最有用的)以及我设法撰写此声明的其他几篇论坛帖子和文章:
Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM
contacts_cstm c_1
JOIN (
SELECT
contacts.first_name,
contacts.las' at line 3
不幸的是,它引发了一个“简单”的语法错误:
Recipe/Ingredients/Ingredient
我已经通过文献和这里的几个帖子进行了广泛的观察,但是我们无法弄清楚这里出了什么问题。我有一种感觉,它与投掷的错误没有任何关系。
绝对赞赏任何和所有帮助。提前谢谢!
答案 0 :(得分:0)
我没有在我的系统上安装MySQL
因此我无法对其进行测试,但它应该与下面的查询类似。此外,您还没有提供表格结构,因此我假设您有一个id
列。
UPDATE contacts_cstm a
JOIN (
SELECT contacts.first_name,
contacts.last_name,
contacts.phone_home,
contacts.phone_mobile,
contacts.phone_work,
contacts.primary_address_street,
contacts.primary_address_city,
contacts.primary_address_state,
contacts.primary_address_postalcode,
contacts.primary_address_country,
opportunities_cstm.stage_c,
contacts_cstm.id_c
contacts_cstm.contact_status_c,
SUM(opportunities_cstm.stage_c = 'Pending' OR opportunities_cstm.stage_c = 'AccountActive' OR opportunities_cstm.stage_c = 'Reinstated' OR opportunities_cstm.stage_c = '%Maturity%' OR opportunities_cstm.stage_c = 'Flagged' OR opportunities_cstm.stage_c = 'Stalled') AS deal_count
FROM opportunities
JOIN opportunities_cstm ON opportunities.id = opportunities_cstm.id_c
RIGHT JOIN contacts ON opportunities_cstm.contact_id_c = contacts.id
JOIN contacts_cstm ON contacts_cstm.id_c = contacts.id
WHERE contacts.deleted = 0 AND opportunities.deleted = 0 AND contacts_cstm.contact_status_c <> 'Lost Client'
GROUP BY contacts.id
HAVING deal_count = 0
) b ON b.id_c = a.id_c
SET a.contact_status_c = 'Lost Client'