Mysql多个连接,返回重复的数据

时间:2013-11-07 10:58:21

标签: mysql inner-join

我的表结构:

Lantern
Lantern_type
Location
Loan
Borrower

我的查询返回重复项,我正在寻找一个不包含重复数据的解决方案。

代码

SELECT
    l.lantern_id, l.lantern_name, l.lantern_state,lt.lantern_type,
    lt.lantern_type_description, lt.lantern_type_tech_info, 
    lt.lantern_type_lens, lo.location_id, lo.location_name, loa.loan_id, 
    loa.loan_start_date, loa.loan_end_date, b.borrower_id, b.borrower_name, b.user_id
FROM lantern as l
INNER JOIN lantern_type as lt
    ON l.lantern_id = lt.lantern_type_id
INNER JOIN location as lo
    ON lt.LANTERN_TYPE_ID = l.lantern_id
INNER JOIN loan as loa
    ON lo.LOCATION_ID = loa.LOAN_ID
INNER JOIN borrower as b
    ON loa.loan_id = b.borrower_id
;

2 个答案:

答案 0 :(得分:1)

我知道这是一个古老的问题,现在可能与您的案件无关,但我不同意接受的答案。在这种情况下,接受的答案就是掩盖问题。

通过查看您的加入声明,您的问题可能就是此加入:

INNER JOIN location as lo
    ON lt.LANTERN_TYPE_ID = l.lantern_id

您不在此连接上使用正确的表别名。我怀疑=之后的别名应该是lo而不是l

答案 1 :(得分:-1)

尝试:

select l.lantern_id, l.lantern_name, l.lantern_state,lt.lantern_type,
    lt.lantern_type_description, lt.lantern_type_tech_info, 
    lt.lantern_type_lens, lo.location_id, lo.location_name, loa.loan_id, 
    loa.loan_start_date, loa.loan_end_date, b.borrower_id, b.borrower_name, b.user_id
From lantern as l
INNER JOIN lantern_type as lt ON
    l.lantern_id = lt.lantern_type_id
INNER JOIN location as lo ON
    lt.LANTERN_TYPE_ID = l.lantern_id
INNER JOIN loan as loa ON 
    lo.LOCATION_ID = loa.LOAN_ID
INNER JOIN borrower as b ON
    loa.loan_id = b.borrower_id
GROUP BY l.lantern_id;

请参阅MySql GROUP BY