2013-12-18 121 views
1

我在我的數據庫中有3個表格,我需要查詢,其結果是Tweet,用戶的暱稱,誰寫了這條推文和pubdate.But我只需要那些我關注的人的推文。在111212_follow表中,我們可以看到follower_id,這是誰在追蹤,我只需要追蹤follower_id後面的人的結果(followed_id)。 Follower_id和followed_id是來自111212_users表的用戶的ID。謝謝大家的幫助。創建幫助所需的SQL查詢

我有1個工作查詢的例子,我不知道如何在我的情況下使用它。

"SELECT tweet_id, poster_id, t112586_users.username as poster, tweet_message, post_date, t112586_users.pics FROM t112586_tweets 
      INNER JOIN t112586_users ON t112586_tweets.poster_id = t112586_users.user_id 
      WHERE poster_id IN(SELECT followee FROM t112586_followers where follower = $id) 
      ORDER BY post_date DESC;" 

更新: 我有工作的查詢,但是這一次不給我的暱稱和pubdate的。 $userId我脫離盤中回吐,這個變量讓我們知道用戶ID,因爲我們在

登錄
"SELECT 111212_tweets.user_id, 111212_tweets.text FROM 111212_tweets INNER JOIN 111212_follow ON 111212_tweets.user_id=111212_follow.followed WHERE 111212_follow.follower='$userId'" 


    --------------------------------------- 
    111212_follow 
    ---------------------------------------  
    follow_id | followed_id | follower_id | 
    --------------------------------------- 
     3  |  2  |  7  | 
    --------------------------------------- 
     5  |  1  |  7  | 
    --------------------------------------- 

    --------------------------------------------- 
    111212_tweets 
    ---------------------------------------------  
     id  | user_id | tweet | pubdate | 
    --------------------------------------------- 
     3   | 2  | text | date | 
    --------------------------------------------- 
     4   | 1  | text | date | 
    --------------------------------------------- 
     5   | 1  | text | date | 
    --------------------------------------------- 

    ---------------------------------------------------------------------------- 
    111212_users 
    ---------------------------------------------------------------------------- 
     id  | name   | nickname | email | password 
    ---------------------------------------------------------------------------- 
     1   | Sten   |  user1  | [email protected] | 123123 
    ---------------------------------------------------------------------------- 
     2   | Mark   |  user2  | [email protected] | 123123 
    ---------------------------------------------------------------------------- 
     7   | John   |  user3  | [email protected] | 123123 
    ---------------------------------------------------------------------------- 

UPDATE:

<ul id="tweetDisplay"> 
      <p>Tweets: </p> 
      <?php 
      $userId = $_SESSION['userId']; 
      $query = "SELECT T1.`tweet`, T2.`nickname`,T1.`pubdate`FROM `111212_tweets` T1 LEFT JOIN `111212_users` T2 ON T1.`user_id` = T2.`id` LEFT JOIN `111212_follow` T3 ON T1.`user_id` = T3.`followed_id` WHERE T3.`follower_id` = '$userId'"; 
      $searchQuery = mysql_query($query); 
      while($row = mysql_fetch_array($searchQuery)){ 
      echo "----------------------------------------------"; 
      echo "<li>Nickname: ".$row['nickname']."</li><li>".$row['text']."</li><li class='date'>".$row['pubdate']."</li>"; 
      } 
      ?> 
     </ul> 
    </div> 
+0

什麼是111212? – Strawberry

+0

@Strawberry只是一個表前綴我假設 – Daveel

+0

是的,111212是前綴 – vladja

回答

0

嘗試此查詢,與用戶的你」的ID替換YOUR_ID重新登錄:

SELECT 
    T1.`tweet`, 
    T2.`nickname`, 
    T1.`pubdate` 
FROM `111212_tweets` T1 
LEFT JOIN `111212_users` T2 
ON T1.`user_id` = T2.`id` 
LEFT JOIN `111212_follow` T3 
ON T1.`user_id` = T3.`followed_id` 
WHERE T3.`follower_id` = 'YOUR_ID' 
+0

做完這個squry後,我試着用'while($ row = mysql_fetch_array($ searchQuery))寫所有的東西{ echo「---------------------- ------------------------「; echo「

  • 暱稱:」。$ row ['nickname']。「
  • 」。$ row ['text']。「
  • 」。$ row ['pubdate']。「
  • 「; '但我有錯誤'警告:mysql_fetch_array()期望參數1是資源,在第54行C:\ xampp \ htdocs \ twitter \ main.php中給出的布爾值是否意味着查詢返回false? – vladja

    +0

    @vladja檢查你的$ searchQuery變量,它必須是一個SQL結果對象,而不是查詢字符串。看看示例#2這裏是如何完成的:http://www.php.net/mysql_fetch_array – Daveel

    +0

    是的,它是。 '$ searchQuery = mysql_query($ query);' – vladja