mysql选择内连接给我循环结果

时间:2013-12-12 11:07:59

标签: mysql inner-join

嗨大家好我难以接受以下mysql查询的问题:

SELECT 
tp.ID AS tpid, 
p.firstName AS pfname, 
p.lastName AS plname, 
p.dob AS pdob, 
fix.homeTeamID AS fixhtid

FROM fixtures AS fix

INNER JOIN teamSeason AS ts ON fix.homeTeamID = ts.ID
INNER JOIN teamPlayers AS tp ON tp.teamSeasonID = ts.ID
INNER JOIN person AS p ON tp.personID = p.ID

WHERE fix.homeTeamID =2

我期望这个查询产生9行,但是有超过180行,原来的9行重复了19次。

我不知道如何解决这个问题,并且想知道是否有人可以帮助我 - 我的大脑很难。

我试图从查询中删除“fix.homeTeamID AS fixhtid”,但它不会停止循环结果。

有没有办法只显示结果而不循环?

我应该在线发布相关的mysql表,以便你看到吗?

谢谢

皮特

更新

我已将我的sql查询更新为:

SELECT 
tp.ID AS tpid, 

p.firstName AS pfname, 
p.lastName AS plname, 
p.dob AS pdob,

fix.homeTeamID AS fixhtid


FROM fixtures AS fix

INNER JOIN teamSeason AS ts 
ON ts.ID=fix.homeTeamID

INNER JOIN teamPlayers AS tp 
ON tp.teamSeasonID = ts.ID

INNER JOIN person AS p 
ON p.ID = tp.personID

WHERE fix.homeTeamID =2

GROUP BY tpid

正如Deepu所建议的那样。我添加了“GROUP BY”,它修复了我的结果。非常感谢Deepu和其他花时间回应的人。

第二次更新

我已将查询再次更新为:

SELECT DISTINCT tp.ID AS tpid,
    p.firstName AS pfname,
    p.lastName AS plname,
    p.dob AS pdob,

    fix.homeTeamID AS fixhtid

    FROM fixtures AS fix

    INNER JOIN teamSeason AS ts
    ON fix.homeTeamID=ts.ID

    INNER JOIN teamPlayers AS tp
    ON tp.teamSeasonID=ts.ID

    INNER JOIN person AS p
    ON tp.personID=p.ID

    WHERE fix.homeTeamID=2
草莓和约翰以最佳实践的名义建议我取代“GROUP BY”代替“SELECT DISTINCT”

谢谢大家。我学到了一些新东西。

1 个答案:

答案 0 :(得分:2)

您希望JOINS根据灯具的原始数据添加数据。然后你应该使用LEFT JOIN

enter image description here