0

我对某些 MySQL 连接为什么会做他们所做的事情的理解有点杂乱无章。

我有多个表,具有 ID 的 ALM_RECORDS 和 ALM_PHONES、ALM_ADDRESS 都有与 ALM_RECORDS 的 ID 列相关的 RecordsID 列。

基本上我需要根据这个 ID 从所有 3 个表中提取。下面是我的查询,但是它不断返回三倍的重复条目。

SELECT a.ID, a.FIRST_NAME, a.LAST_NAME, 
       b.RecordID, b.PHONESID, b.PhoneType, b.NUM,        
       c.RecordID, c.address_block, c.city, c.state, c.post_code, c.country
FROM ALM_RECORDS a
    LEFT JOIN ALM_PHONES b
        ON a.ID = b.RecordID
    LEFT JOIN ALM_ADDRESS c
        ON a.ID = c.RecordID
WHERE a.ID = "123456"

结果应该是:

123456 - John - Smith - Business - 111.222.3333 - address etc..
123456 - John - Smith - Home     - 444.555.6666 - address etc..

但我得到的是:

123456 - John - Smith - Business - 111.222.3333 - address etc..
123456 - John - Smith - Business - 111.222.3333 - address etc..
123456 - John - Smith - Business - 111.222.3333 - address etc..
123456 - John - Smith - Home     - 444.555.6666 - address etc..
123456 - John - Smith - Home     - 444.555.6666 - address etc..
123456 - John - Smith - Home     - 444.555.6666 - address etc..

有人可以向我解释我在这里缺少什么吗?我应该使用不同类型的连接吗?

4

1 回答 1

0
SELECT a.ID, a.FIRST_NAME, a.LAST_NAME, 
       b.RecordID, b.PHONESID, b.PhoneType, b.NUM,        
       c.RecordID, c.address_block, c.city, c.state, c.post_code, c.country
FROM ALM_RECORDS a
    LEFT JOIN ALM_PHONES b
        ON a.ID = b.RecordID
    LEFT JOIN ALM_ADDRESS c
        ON a.ID = c.RecordID
WHERE a.ID = "123456" GROUP BY b.NUM
于 2013-09-13T18:23:43.727 回答