为什么这个MySQL加入声明不起作用?

时间:2010-05-04 01:29:24

标签: php mysql

这是我的代码:

$query2 = mysql_query("SELECT * FROM categories WHERE parent = $id JOIN SELECT * FROM posts WHERE main_nav_page = '$idTwo'");

                    while ($row2 = mysql_fetch_assoc($query2)) {
                        $id   = $row2['id'];
                        $name = $row2['name'];
                        $slug = $row2['slug'];
                        $subMenuOrder = $row2['sub_menu_order'];

                        echo "<tr>\n";
                        echo "<td>&nbsp; -- $name</td>\n";
                        echo "</tr>\n";
                    }

我的语法错了吗?

编辑:

错误信息是:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/studentw/public_html/new_nav.php on line 30

2 个答案:

答案 0 :(得分:4)

而不是你可能想要更像的东西:

SELECT * 
FROM 
       categories c 
    INNER JOIN 
       posts p ON c.categoryid = p.categoryid 
WHERE 
       c.parent = $id 
       AND p.main_nav_page = '$idTwo'; 

请注意,表是连接的,而不是select语句。此外,联接在FROM子句中指定。

试试这个:

$results = mysql_query("query here") or die(mysql_error());

答案 1 :(得分:1)

我认为语法错误很小。看起来你错过了WHERE parent - 子句中“$ id”周围的单引号('')。应该是这样的,我猜:

SELECT * FROM categories WHERE parent = '$id' ...