我有这个问题:
SELECT w.windate, w.wintime, w.field, w.htname, w.vtname, w.plateumpire,
w.fieldumpire1, w.fieldumpire2, w.fieldumpire3, p.pteamname, p.teamcoach,
p.pteamdivision, d.originalid
FROM sportsdb_wins AS w, playerteams AS p, sportsdb_divs AS d
WHERE p.teamcoach='$coachid'
AND p.pteamname IN (w.htname, w.vtname)
AND p.forteam='$teamid'
并且至少有一行返回3次!为什么?请帮忙我是SQL新手。 在PHP中使用它...
答案 0 :(得分:6)
必须加入表格时使用JOIN。您当前的问题是所谓的笛卡尔积。
答案 1 :(得分:1)
您没有为sportsdb_divs
表定义连接条件,因此您将获得这些结果的交叉产品。如果您使用显式连接(您应该在所有SQL代码中执行此操作),这将更加明显。
SELECT w.windate, w.wintime, w.field, w.htname, w.vtname, w.plateumpire,
w.fieldumpire1, w.fieldumpire2, w.fieldumpire3, p.pteamname, p.teamcoach,
p.pteamdivision, d.originalid
FROM playerteams AS p
INNER JOIN sportsdb_wins AS w
ON p.teamname in (w.htname, w.vtname)
INNER JOIN sportsdb_divs AS d
ON ?????
WHERE p.teamcoach='$coachid'
AND p.forteam='$teamid'