0

已经有许多左连接问题,但我仍然不能完全解决这个问题。WHERE 条件看起来不适合移动。

问题是应该返回 4 行,但只有 1 行。

在检查左连接条件时,每个左连接返回 1 行,这对于表中的记录数是正确的,但是下面的查询返回 1 记录而不是 4,但我看不到如何返回 4 , 然而。

查询如下:(给出 1 个结果而不是 4 个;预期为 4 个)

SELECT 
 list.uid,
 list.business_uid,
 list.creator_name,
 business.company_name,
 list_alias.uid AS list_alias_uid,
 list_alias.alias AS list_alias,
 list_member.uid AS list_member_uid,
 mailbox.full_name AS list_member_name,
 mailbox.email_address AS list_member_email_address
FROM
 mailbox,
 business,
 list
LEFT JOIN
 list_alias ON list_alias.list_uid=list.uid
LEFT JOIN
 list_member ON list_member.list_uid=list.uid
WHERE
 list.business_uid='1'
 AND list.business_uid=business.uid
 AND mailbox.uid=list_member.mailbox_uid
ORDER BY
 list.full_name ASC 

数据:

业务 UID 1 有 4 个列表

SELECT * FROM list WHERE business_uid=1-- 给出 4 个结果

SELECT * FROM list_alias WHERE list_uid IN (SELECT uid FROM list WHERE business_uid=1)-- 给出 1 个结果

SELECT * FROM list_member WHERE list_uid IN (SELECT uid FROM list WHERE business_uid=1)-- 给出 1 个结果

欢迎任何关于我可以检查的内容的指示。

表样本数据:

列表:

uid | business_uid | creator_name | full_name
--------------------------------------------------
 1          1         List Maker   Subscribe to W
 2          1         List Maker   Subscribe to X
 3          1         List Maker   Subscribe to Y
 4          1         List Maker   Subscribe to Z

商业:

uid | company_name
-------------------
 1    List Company

列表别名:

uid | list_uid | alias
----------------------------------------
 1       1       subscriber@list-url.com

列表成员:

uid | list_uid | mailbox_uid
------------------------------------
 1       1       1

邮箱:

uid | full_name | email_address
-------------------------------
 1     I am He    me@me.com
4

2 回答 2

1

尝试使用单一连接方法,就像这样。

SELECT list.uid,
    list.business_uid,
    list.creator_name,
    b.company_name,
    la.uid AS list_alias_uid,
    la.alias AS list_alias,
    lm.uid AS list_member_uid,
    m.full_name AS list_member_name,
    m.email_address AS list_member_email_address
FROM list LEFT JOIN list_member lm ON lm.list_uid=list.uid
    LEFT JOIN mailbox m ON m.uid=lm.mailbox_uid
    LEFT JOIN business b ON list.business_uid=b.uid
    LEFT JOIN list_alias la ON la.list_uid=list.uid
WHERE list.business_uid=1
ORDER BY list.full_name ASC 
于 2013-03-19T01:03:46.387 回答
0

问题:'list' 表中的 'uid' 的值是什么?因为“uid”与“business_uid”不同。我的意思是 ...

如果“列表”表有这个......

'uid' 'business_uid' 1 1 2 1 3 1 4 1

那么这就是问题所在。您返回的是相同的 'busines_uid' 但不同的 'uid' 这意味着它只会匹配第一条记录。

于 2013-03-19T00:47:34.017 回答