0

我的表结构:

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
;
4

2 回答 2

1

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

通过查看您的加入声明,您的问题可能与此加入有关:

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

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

于 2015-01-16T14:52:11.133 回答
-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 分组依据

于 2013-11-07T11:01:09.217 回答