1

我正在一个网站上显示两支球队的足球比分和我的数据库中的两个分数。

这是我的代码:

<?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
        }
        }
        ?>

我得到:

选择错误:不是唯一的表/别名:“滞后”

当我尝试:

INNER JOIN lag s ON resultat.lag1 = lag.id
INNER JOIN lag c ON resultat.lag2 = lag.id

我得到:

SELECT 错误:“on 子句”中的未知列“lag.id”

我该怎么做?来自同一个表的两个连接

4

3 回答 3

4

你需要使用你刚刚给你的表的别名lags您在第一次加入时分配了c第二次,因此您应该使用它们

INNER JOIN lag s ON resultat.lag1 = s.id
INNER JOIN lag c ON resultat.lag2 = c.id
于 2013-06-13T16:03:30.940 回答
1

您可以尝试像这样组合 JOIN:

INNER JOIN lag ON resultat.lag1 = lag.id OR resultat.lag2 = lag.id

如果需要,将 OR 替换为 AND

于 2013-06-13T16:05:06.680 回答
0

这是一个自加入,你需要在你的 expession 中使用别名:

     INNER JOIN lag s ON resultat.lag1 = s.id
     INNER JOIN lag c ON resultat.lag2 = c.id 

通常,您将自连接与 adjacency-list 一起使用,并沿着一个维度遍历树:

    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 
于 2013-06-13T16:16:34.363 回答