2012-05-08 18 views
0

作爲一名新手,我有點失落至於如何釘這個。我正試圖加入三張桌子。這是表結構:如何連接三個表並添加「AND」運算符?

Accounts Table: 

1) id 
2) User_id (Id given to user upon sign up) 
3) Full_name 
4) Username 
5) Email 
6) Password 

Contacts Table: 

1) My_id (This is the user who added friend_id) 
2) Contact_id (this is the contact who was added by my_id) 
3) Status (status of relationship) 

Posts Table: 

1) Post_id 
2) User_posting (this is the user who posted it) 
3) Post_name 
4) User_allowed (this is where its specified who can see it) 

下面是代碼結構,我有:

<?php 

$everybody = "everybody"; 
$sid = "user who is logged in."; 

$sql = <<<SQL 
SELECT DISTINCT contacts.contact_id, accounts.full_name, posts.post_id, posts.post_name 
FROM contacts, accounts, posts 
WHERE 
    (contacts.my_id = '$sid' 
     AND contacts.contact_id = accounts.user_id) 
    OR (contacts.my_id = '$sid' 
     AND contacts.contact_id = accounts.user_id) 
    OR (posts.user_posting = '$sid' 
     AND contacts.contact_id = accounts.user_id 
     AND posts.user_allowed = '$everybody') 
    OR (posts.user_id = '$sid' 
    AND contacts.user_id = accounts.user_id 
    AND posts.user_allowed = '$everybody') 

LIMIT $startrow, 20; 


$query = mysql_query($sql) or die ("Error: ".mysql_error()); 

$result = mysql_query($sql); 

if ($result == "") 
{ 
echo ""; 
} 
echo ""; 


$rows = mysql_num_rows($result); 

if($rows == 0) 
{ 
print(""); 

} 
elseif($rows > 0) 
{ 
while($row = mysql_fetch_array($query)) 
{ 

$contactid = htmlspecialchars($row['contact_id']); 
$name = htmlspecialchars($row['full_name']); 
$postid = htmlspecialchars($row['post_id']); 
$postname = htmlspecialchars($row['post_name']); 

// If $dob is empty 
if (empty($contactid)) { 

$contactid = "You haven't added any friends yet."; 
} 


print("$contactid <br>$postname by $name <br />"); 
} 

} 

的問題是,查詢顯示我我的職位,加上朋友的所有帖子。

我在做什麼錯了?

+0

您應該使用正確的'JOIN'語法。你的預期產出是多少? – Matthew

+0

您期待的輸出是什麼? – Travesty3

+0

你的輸出有什麼問題?期望什麼? – Paul

回答

1

假設你只是想顯示你的朋友的帖子(而不是你自己的):

$everybody = "everybody"; 
$sid = user who is logged in. 

$sql = "SELECT a.User_id, a.Full_name, p.Post_id, p.Post_name 
FROM posts p 
INNER JOIN accounts a ON a.User_id = p.User_posting 
INNER JOIN contacts c ON c.My_id = '$sid' AND c.Contact_id = a.User_id 
WHERE p.User_allowed = '$everybody' 
LIMIT $startrow, 20"; 

編輯:解釋查詢。

  1. 該查詢用於輸出
  2. FROMpostsSELECT條某些字段,因爲結果所包含的每個柱的行。
  3. 那基表JOIN版與accounts表來獲取發表用戶的數據和
  4. 另外JOIN版與contacts表,這樣我們就可以進行過濾,只已發佈,同時也是朋友們所涉及的用戶添加的用戶。
  5. 一個額外的過濾器是在允許的值和
  6. 爲分頁的目的,我們有一個由$startrow定義的偏移量。
+0

好吧,滾動調試會話現在停止。 – casperOne

+0

我如何查看以前的評論?我需要他們來幫助我的查詢。 – ariel

1

應該是這樣(不徹底的檢查):

SELECT DISTINCT contacts.contact_id, 
       accounts.full_name, 
       posts.post_id, 
       posts.post_name 
FROM contacts 
INNER JOIN accounts ON accounts.id = contacts.id 
INNER JOIN posts ON posts.User_posting = accounts.id /* (where'd get posts.user_id btw) */ 
WHERE contacts.my_id = $sid 
     AND posts.user_allowed = $everybody 
LIMIT $startrow, 20 
+0

嘿,我試過你的,它說錯誤:未知列'accounts.id'在'子句' – ariel

+0

你不在帳戶表上有id字段嗎?你能提供你的數據庫結構嗎? –

+0

是的,我確實......但它超越了我爲什麼沒有檢測到它。數據庫結構正如我上面張貼.. – ariel