我正在尝试获取与该公司相关联的相应联系人的所有公司的列表。为了论证,我假设每家公司只存在一个联系人。我正在尝试使用以下内容:
SELECT companies.id as company_id, contacts.name, contacts.email
FROM companies, (SELECT * FROM contacts WHERE companyId = company_id LIMIT 1) as contacts
但是我收到了这个错误:
我几乎尝试过对列本身和表格的各种变化,但整个上午都没有运气。有什么想法吗?
由于
答案 0 :(得分:0)
为什么不这样做:
SELECT cmp.id as company_id,
(SELECT name FROM contacts WHERE cmp.Id = company_id LIMIT 1)
FROM companies cmp
答案 1 :(得分:0)
如果代码中存在电子邮件,则可以添加电子邮件
SELECT comp.id as company_id,
(SELECT name,email FROM contacts WHERE comp.Id = company_id LIMIT 1) FROM companies comp
如果您有可以更改的电子邮件值的不同字段名称,则电子邮件是字段名称
这样做
SELECT comp.id as company_id, SELECT name,email FROM contacts WHERE comp.Id = company_id LIMIT 1 FROM companies comp