我正在尝试使用以下sql命令将3个表连接在一起:
SELECT
tg.Kickoff,
tg.League,
tg.home,
tg.away,
ln.Full,
ln.Mysite,
ln.Country,
tl.mydb
FROM `todaysgames` AS tg
INNER JOIN `leaguenames` AS ln ON tg.League = ln.Othersites
INNER JOIN `teamlookup` AS tl ON tg.home = tl.websitedb
OR tg.away = tl.websitedb;
问题是我得到的是每行加倍。
我想要做的是每个tg.home和tg.away将其替换为tl.websitedb(这基本上修改了名称)。
这可能吗? (以下是当前输出)
答案 0 :(得分:2)
如果我正确地解释了你的问题,那么每场比赛你会得到两条你只想要一条的记录?这是因为你在两个条件下加入teamlookup,因此每场比赛找到两条记录。
示例:
todaysgames
Game Home Away 1 A B
teamlookup
websitedb mydb A DBA B DBB
结果
Game Home Away mydb 1 A B DBA 1 A B DBB
我想你真的想要得到这个:
Game Home Away mydbHome mydbAway 1 A B DBA DBB
查询:
SELECT
tg.Kickoff,
tg.League,
tg.home,
tg.away,
ln.Full,
ln.Mysite,
ln.Country,
tlh.mydb as mydb_home,
tla.mydb as mydb_away
FROM todaysgames tg
JOIN leaguenames ln ON tg.League = ln.Othersites
JOIN teamlookup tlh ON tg.home = tlh.websitedb
JOIN teamlookup tla ON tg.away = tla.websitedb;