[英]Two JOINS on same table
I'm working on a website which shows the soccer score with two teams and two scores in my DB. 我在一个网站上工作,该网站在我的数据库中显示了两支球队的足球得分和两分。
This is the code I have: 这是我的代码:
<?php
$result = mysql_query( "SELECT * FROM resultat
LEFT JOIN brukere ON resultat.dommer = brukere.id
INNER JOIN lag ON resultat.lag1 = lag.id
INNER JOIN lag ON resultat.lag2 = lag.id
ORDER BY slutt DESC
" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);if ($result) {while ($row = mysql_fetch_array($result)) {
$_SESSION['id']=$row['id'];
?>
<tr>
<td><?php echo $row['klubb']; ?></td>
<td><?php echo $row['klubb']; ?></td>
<td><?php echo $row['stilling1'] . " - " . $row['stilling2']; ?></td>
<td><?php echo $row['roed'] . " rødt og " . $row['gult'] . " gult"; ?></td>
<td><?php echo $row['bane']; ?></td>
<td><?php echo $row['navn']; ?></td>
</tr>
<?php
}
}
?>
And I get: 我得到:
SELECT Error: Not unique table/alias: 'lag'
选择错误:不是唯一的表/别名:'滞后'
When I try: 当我尝试:
INNER JOIN lag s ON resultat.lag1 = lag.id
INNER JOIN lag c ON resultat.lag2 = lag.id
I get: 我得到:
SELECT Error: Unknown column 'lag.id' in 'on clause'
SELECT错误:“ on子句”中的未知列“ lag.id”
How do I do it? 我该怎么做? Two joins from the same table
来自同一表的两个联接
You need to use the alias you just gave to your table lag
. 您需要使用刚赋予表
lag
的别名。 You assigned s
at first join and c
at second so you should use them 您在第一次加入时分配了
s
,在第二次加入时分配了c
,因此您应该使用它们
INNER JOIN lag s ON resultat.lag1 = s.id
INNER JOIN lag c ON resultat.lag2 = c.id
You can try to combine JOIN like this: 您可以尝试将JOIN这样合并:
INNER JOIN lag ON resultat.lag1 = lag.id OR resultat.lag2 = lag.id
Replace OR with AND if you need 如果需要,请用AND替换OR
This is a self-join and you need the alias in your expession: 这是一个自联接,并且在expession中需要别名:
INNER JOIN lag s ON resultat.lag1 = s.id
INNER JOIN lag c ON resultat.lag2 = c.id
In general you use self-joins with adjacency-list and to walk the tree down a dimension: 通常,您将自连接与邻接列表一起使用,并将树沿维度移动:
SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
LEFT JOIN category AS t4 ON t4.parent = t3.category_id
WHERE t1.parent is NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.