SQL Query创建所需的帮助

时间:2013-12-18 00:31:08

标签: mysql

我的数据库中有3个表,我需要查询,结果将是Tweet,用户的昵称,谁写了这个推文和pubdate.But我只需要那些我正在关注的人的推文。在111212_follow表中我们可以看到follower_id,这是谁跟随,我只需要关于follower_id跟随(follow_id)的人的结果。 Follower_id和followed_id是来自111212_users表的用户ID。谢谢大家的帮助。

我有一个工作查询的例子,我不知道在我的情况下如何使用它。

"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     | t1@t.com  |  123123
    ----------------------------------------------------------------------------
       2         |    Mark         |      user2     | t2@t.com  | 123123
    ----------------------------------------------------------------------------
       7         |    John         |      user3     | t3@t.com  | 123123
    ----------------------------------------------------------------------------

更新:

<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>

1 个答案:

答案 0 :(得分:0)

尝试此查询,将YOUR_ID替换为您登录的用户的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'