我在我的數據庫中有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>
什麼是111212? – Strawberry
@Strawberry只是一個表前綴我假設 – Daveel
是的,111212是前綴 – vladja