开发者

SQL Statement (INNER JOIN ?)

I have two tables wnews and categories.

I wanna print the names of categories by id

something like wnews.category (is an integer) and categories (id, name) where wnews.category = cateroies.id and print name

I made something but I failde ...

$query = mysql_query("SELECT wnews.id, wnews.torrentid, wnews.title,
                             wnews.img, wnews.added, wnews.category,
                             wnews.genre, wnews.uploader, wnews.description,
                             categories.id, categories.name
                      FROM wnews
                        INNER JOIN categories
                          ON wnews.category=categories.id 
                      ORDER BY wnews.added DESC")
         or sqlerr();

while ($arr = mysql_fetch_assoc($query))
{ printNews($arr["wnews.torrentid"],开发者_JAVA技巧 $arr["wnews.title"], 
            $arr["wnews.img"],       $arr["wnews.added"],
            $arr["categories.name"], $arr["wnews.genre"], 
            $arr["wnews.uploader"],  $arr["wnews.description"]);
} 

Thanks for your time.

Solved.

        $query = mysql_query("SELECT wnews.id, wnews.torrentid, wnews.title, wnews.img, wnews.added,
        wnews.category, wnews.genre, wnews.uploader, wnews.description, categories.id, categories.name AS cat_name FROM wnews
        LEFT JOIN categories ON wnews.category = categories.id
        ORDER BY wnews.added DESC") or die(mysql_error());
        while ($arr = mysql_fetch_assoc($query))
        {
            printNews($arr["torrentid"], $arr["title"], $arr["img"], $arr["added"], $arr["cat_name"], $arr["genre"], $arr["uploader"], $arr["description"]);
        }


I like using the MySQL Query Browser when creating complex sql statements (with inner joins and such).

You should be able to download it here: http://dev.mysql.com/downloads/

Shannon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜