0

我对我的表使用了自联接,但我无法检索表行

$SQL1 = "SELECT e1.userid, e2.userid
FROM userfield AS e1, userfield AS e2
WHERE e1.field5 = e2.field5 and e1.field5!=' ' and e2.field5!=' ' and e1.userid!=e2.userid";

$result2 = mysql_query($SQL1);

while ($db_field2 = mysql_fetch_assoc($result2)) {
    print $dbfield2[userid];  
}

当我执行此代码时,它只显示空白页。当我在 msql 数据库中触发此查询时,它返回两个用户 ID

任何帮助将不胜感激

4

2 回答 2

0

你必须给他们这样的别名:

$SQL1 = "SELECT e1.userid AS userid1, e2.userid AS userid2
         FROM userfield AS e1, userfield AS e2
         WHERE e1.field5 = e2.field5 and e1.field5!=' ' and e1.userid!=e2.userid";

$result2 = mysql_query($SQL1);

while ($db_field2 = mysql_fetch_assoc($result2)) {
    print $dbfield2['userid1'];  
}

请考虑切换到 PDO oder mysqli。所有 mysql_* 函数都被正式标记为已弃用。

于 2012-12-14T11:24:50.843 回答
0
$SQL1 = "SELECT e1.userid, e2.userid
    FROM userfield AS e1, userfield AS e2
    WHERE e1.field5 = e2.field5 and e1.field5!=' ' and e1.userid!=e2.userid";

$result2 = mysql_query($SQL1);

while ($db_row = mysql_fetch_assoc($result2)) {
    print $db_row['e1.userid'];  
}

PS您不需要and e2.field5!=' 'inWHERE子句,因为e2.field5必须等于e1.field5and e1.field5!=' '

于 2012-12-13T19:47:35.803 回答