我有如下表格:
ORG
id Name media_budget p_contact s_contact
------ ------- ------------ --------- -----------
1 FIFA 4654623 1 3
2 FIFA_AP 1234 2 3
3 ICC 203254 3 2
USERS
id fname lname contact email
------ ------- ------ ------- -----------------
1 JHONE MICHEL 124552 email@gmail.com
2 William Martin 254623 gmail@email.com
3 Ann Lee 321546 lee@gmail.com
我想为ORG中的每一行选择ORG.con_contact和ORG.s_contact的user.fname,以下语句与ORG中的单个记录一起工作正常,但是当查询返回多行时会抛出错误
"Error Code: 1242
Subquery returns more than 1 row"
我的陈述是:
SELECT o.Name, o.media_budget,
(SELECT u.fname AS primaryName FROM org o LEFT OUTER JOIN users u ON o.p_contact = u.id) AS PrimaryContact,
(SELECT usr.fname AS SecondaryName FROM org og LEFT OUTER JOIN users usr ON og.s_contact = usr.id) AS SecondaryContact
FROM org o;
在子查询中应该改变什么来实现期望结果:
id Name media_budget PrimaryContact SecondaryContact
------ ------ ------------ -------------- ----------------
3 ICC 203254 Ann William
4 FIFA 2656 JOHN Ann