我需要關於JOIN表獲得幫助,從其他表中獲取類別名稱&作者姓名。來自不同表格的查詢如何加入建議PLease(PHP)(SQL)
的文章的網站我有三個表
- 文章
- 類別
- 用戶
文章表結構:
id
title
slug
content
category
author
date
我保存類別ID在文章表用戶ID與每篇文章
同時顯示文章我用這個類別: (只是一些想法草案不完整的代碼)
localhost/testsite/category.php?cat=category-slug
$catslug = ($_GET["cat"]);
//GET CAT ID from category table from slug
$qry = "SELECT * from category WHERE slug='$catslug';
$res = mysql_query($qry) or die("Error in Query");
$ans = mysql_fetch_array($res);
$cid = $ans['id'];
$catname = $ans['title'];
<h2><?php echo $catname; ?></h2>
//after getting cat-id query to get article of that ID
$aqry = select * from article where category=$cid ORDER BY date";
$ares = mysql_query($aqry) or die("Error in Query");
$aans = mysql_fetch_array($ares))
$aid = $aans['author'];
//then another query to get author name from users
select username from users where id=$aid
我第m個學習PHP &沒有多少熟悉JOIN聲明&合併查詢 需要幫助/建議hw我可以通過JOIN表實現相同的操作,而不是使用不同的查詢來從表中獲取catname和作者姓名。
感謝
請嘗試解釋此查詢到我 我知道加入,但沒有太多關於不同 – Vehlad 2012-03-22 13:27:43
不同將獲得一個唯一的字段出現時消除重複。就像你會和團隊一樣。說實話,我不記得mysql是否使用這種語法,或者這些字段必須放在parentesis中。 – Iridio 2012-03-22 13:37:24
非常感謝方向我有了一些變化:))得到它 去除不同,因爲我想從該類別 還刪除U.id = A.author條件獲取所有的文章我想列出不但任何單個作者的所有文章從用戶表中選擇作者姓名。 – Vehlad 2012-03-22 17:13:39