2012-03-22 30 views
0

我需要關於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和作者姓名。

感謝

回答

0

這應該做

SELECT distinct c.title, U.Username 
from category C join article A on A.category=C.id 
join users U on U.id=A.author 
WHERE C.slug= :catslug 
+0

請嘗試解釋此查詢到我 我知道加入,但沒有太多關於不同 – Vehlad 2012-03-22 13:27:43

+0

不同將獲得一個唯一的字段出現時消除重複。就像你會和團隊一樣。說實話,我不記得mysql是否使用這種語法,或者這些字段必須放在parentesis中。 – Iridio 2012-03-22 13:37:24

+0

非常感謝方向我有了一些變化:))得到它 去除不同,因爲我想從該類別 還刪除U.id = A.author條件獲取所有的文章我想列出不但任何單個作者的所有文章從用戶表中選擇作者姓名。 – Vehlad 2012-03-22 17:13:39

0

假設類別ID是你想做的事,文章表外JEY

嘗試此查詢

select username from users 
where id in (
select author from category cat 
inner join article art 
on cat.category_id =art.category_id 
where cat.slug= $cat 
order by art.date desc) 
+0

類別ID,作者ID沒有外鍵在文章表 hmmmmmm 我應該建立外鍵的文章,以加快進程。 我將與此 – Vehlad 2012-03-22 13:26:05

0
$q = "SELECT article.* 
     FROM article 
      LEFT JOIN category 
      ON article.category = category.ID 
      LEFT JOIN users 
      ON article.author = users.ID 
     WHERE article.slug = :slug"; 

:slug在這裏,你會(如果你使用PDO綁定)插入$_GET['slug']

+0

我試圖 '$ q =「選擇文章。*從物品 \t \t \t LEFT JOIN類別 \t \t \t上articles.category = categories.id \t \t \t LEFT JOIN用戶 \t \t \t on articles.author = users。ID \t \t \t WHERE categories.slug =「$貓」「;' 我改變WHERE category.slug因爲我想通過$ _GET通過塞從單一的品類查詢物品$貓 這導致在查詢錯誤 – Vehlad 2012-03-22 16:41:52

0

首先,你應該寫一個存儲過程......但這裏是你如何度日的categoryID同時包含類名和用戶名的文章。

如下表結構:

article 
--------- 
articleID <- primary key 
title 
slug 
content 
categoryID <- foreign key 
userID <- foreign key 
createdDT 

category 
-------- 
categoryID <- primary key 
categoryName 

user 
-------- 
userID <- primary key 
userName 

下面介紹如何使用連接到編寫查詢:

SELECT a.title, a.slug, a.content, a.createdDT, c.categoryName, u.userName 

FROM dbo.article a 

JOIN dbo.category c on a.categoryID = c.categoryID 
JOIN dbo.user u on a.userID = u.userID 

WHERE a.categoryID = @categoryID 

下面是來自微軟的文章連接 - http://msdn.microsoft.com/en-us/library/ms191517.aspx

+0

這裏@CategoryID是$ _GET的值? – Vehlad 2012-03-22 16:30:30

+0

如果你創建了一個存儲過程,@CategoryID將是輸入參數之一,如果你使用內聯查詢,那麼是的。 – 2012-03-22 17:16:30

0

你可以做一些與SQL請求不同的事情。你可以做一些加入您的SQL請求,但你也可以做另一個SELECT語句在你的要求就像

SELECT SOMETHINGS FROM A_TABLE WHERE (SELECT ANOTHER_THING FROM ANOTHER_TABLE WHERE VALUE =1 

但是,如果你在你的第二個請求

1分的結果。如果你想這種說法只會工作連接兩個表一定的價值,你必須做這樣

SELECT something FROM a_table AS alias1 JOIN another_table AS alias2 
ON alias1.value1 = alias2.value1 and ..... 

您可以比較所有你從一個表要到另一個值,你也可以加入兩個以上的表和ON關鍵詞不是通過sql語法請求的,你可以只做

SELECT * FROM table_1 join table_2 

,它會告訴你從兩個表中的所有thje數據,但可以肯定,這是你想要這種小的查詢可以有一些大的成果,減緩您的應用程序

您可以讀取數據更多有http://dev.mysql.com/doc/refman/5.0/en/join.html