簡體   English   中英

SQL 語句(INNER JOIN?)

[英]SQL Statement (INNER JOIN ?)

我有兩個表 wnews 和類別。

我想按 id 打印類別名稱

類似 wnews.category (是一個整數)和類別(id、name)的東西,其中 wnews.category = cateroies.id 和打印名稱

我做了一些東西,但我失敗了...

$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"], $arr["wnews.title"], 
            $arr["wnews.img"],       $arr["wnews.added"],
            $arr["categories.name"], $arr["wnews.genre"], 
            $arr["wnews.uploader"],  $arr["wnews.description"]);
} 

謝謝你的時間。

解決了。

        $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"]);
        }

在創建復雜的 sql 語句(帶有內部連接等)時,我喜歡使用 MySQL 查詢瀏覽器。

你應該可以在這里下載它: http://dev.mysql.com/downloads/

香農

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM