执行 SQL:
SELECT
customer_name,
COUNT(id) AS count_child
FROM
scrm_customer r
LEFT JOIN scrm_contacts c ON
r.contacts_id = c.contacts_id
GROUP BY
customer_name
出现错误: Column 'customer_name' in field list is ambiguous
原因是:scrm_customer 和 scrm_contacts 两张表都有 customer_name 字段,不知道按照哪张表的 customer_name 进行分组。
ambiguous = adj. 模棱两可的;含混不清的;不明确的
正确 SQL,多表联合查询,最后都加上表限定,不容易出错:
SELECT
r.customer_name,
COUNT(id) AS count_child
FROM
scrm_customer r
LEFT JOIN scrm_contacts c ON
r.contacts_id = c.contacts_id
GROUP BY
r.customer_name