2014-05-12 76 views
0

好吧,我會試着重新解釋這個問題,因爲沒有人能夠理解我的目標。這是凌晨3點,所以這可能是我鬆懈的地方...但我會盡力在這裏。加入第三個MySQL表(重做)

我目前在郵編&評論部分,用戶可以發表在自己或其他用戶的牆上,因此,其他用戶可以對這些帖子發表評論。與社交網絡狀態或一般論壇相同的東西。我有3個表...

Users 
Statuses 
Comments 

用戶包含標準...

userid 
username 
first_name 
last_name 
photo 

以下狀態包含了從每個用戶發佈一個主體地位的任何信息,這些狀態會顯示在用戶的配置文件。因此,用戶1可以在用戶2的個人資料上發佈狀態。下面是狀態表的設計...

status_id (auto-i) 
user_id (The users ID whos profile the post was added too) 
sender_id (The user who sent the post or wrote it) 
date (Date/Time was sent) 
rate (This doesn't matter for a moment) 
comments (This will count all the comments and display the number) 
status (The actual status written out) 

這些表的罰款加在一起在我的腳本連接兩個表並顯示在用戶的信息(誰張貼的狀態之一),如個人資料照片和姓名等...這是我目前的劇本,根本沒有任何問題...

//// GET STATUSES 
            $user_id = $profile_data['userid']; 
           $data = mysql_query(" 
           SELECT 
        statuses.status_id, 
        statuses.user_id, 
        statuses.sender_id, 
        statuses.date, 
        statuses.rate, 
        statuses.comments, 
        statuses.status, 
        users.userid, 
        users.username, 
        users.first_name, 
        users.last_name, 
        users.photo 
           FROM 
            statuses 
           LEFT JOIN 
            users 
           ON 
            statuses.sender_id=users.userid 
           WHERE 
            statuses.user_id = '{$profile_data['userid']}' 
           ORDER BY 
           `statuses`.`date` DESC 
           ") or die(mysql_error()); 

            while($status = mysql_fetch_array($data))//added this 
            { 
            $statusid = $status['status_id']; 
            $date = $status['date']; 
            $rate = $status['rate']; 
            $comments = $status['comments']; 
            $userid = $status['user_id']; 
            $senderid = $status['sender_id']; 
            $statusbody = $status['status']; 
            $username = $status['username']; 
            $firstname = $status['first_name']; 
            $lastname = $status['last_name']; 
            $photo = $status['photo']; 

            ?> 
<form action="" method="POST" role="form" enctype="multipart/form-data" id="statusupdate" class="facebook-share-box"> 
      <div class="share"> 
       <div class="panel panel-default"> 
         <div class="panel-heading"><a href="<? echo 'http://basecentre.co.uk/',$status["username"]; ?>"><img alt="" align="left" hspace="20" height="70" width="70" src="<? echo 'http://basecentre.co.uk/userimages/',$status["photo"]; ?>"> </a> <font size="+2"><i class="icon icon-comment-o"></i></font> <a href="<? echo 'http://basecentre.co.uk/',$status["username"]; ?>"><font size="+2"><?php echo $status['first_name']; ?> <?php echo $status['last_name'] ; ?></font></a> | <i class="icon icon-clock-o"></i> <a rel="tooltip" href="#" data-original-title="<? echo "". date('F j, Y, g:i a', strtotime($status['date']) + 60*60) .""; ?>"><?php echo "<strong>". date('j F', strtotime($status['date']) + 60*60) ."</strong>"; ?></a></div> 
         <div class="panel-body"> 
         <div class=""> 
          <?php echo $status['status']; ?> 
         </div> 
         </div> 
         <div class="panel-footer"> 
           <div class="row"> 
           <div class="col-md-7"> 
           Comment | Like 
           </div> 
           </div> 
         </div> 
        </div> 
      </div> 
     </form> 
           </br> 


<? 
            } 


    ?> 

我已經爲評論構建了一張新表。所以,我可以去一個用戶配置文件,讀了「狀態」,然後我可以給它添加評論...這裏是新表...

comment_id (auto-i) 
status_id (added depending on which status you comment on. If the comment is on status id #5, the same number will be sent to this to connect the comments with the correct statuses) 
sender_id (the id of the user who is sending the comment which would be $_session['userid']; 
date (date the comment was sent) 
rate (Doesn't matter yet) 
comment (the actual comment written out). 

我需要加入新的意見表另外兩張桌子,不知何故?我嘗試添加另一個「左連接」,但這不起作用?我對這個東西不太滿意。 我的目標是按發佈日期的順序,正確顯示每個評論的連接狀態。最近在底部...顯示用戶信息,以便知道是誰發佈了評論。

希望這會給你更多的理解,我希望有人能幫助!對不起,它很長,但你們要求更多的信息.​​..謝謝!

編輯附加數據庫記錄:

狀態表:

CREATE TABLE IF NOT EXISTS `statuses` (
    `status_id` int(25) NOT NULL AUTO_INCREMENT, 
    `user_id` int(25) NOT NULL, 
    `sender_id` int(25) NOT NULL, 
    `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `rate` int(25) NOT NULL DEFAULT '0', 
    `comments` int(25) NOT NULL DEFAULT '0', 
    `status` text COLLATE utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`status_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=30 ; 

-- 
-- Dumping data for table `statuses` 
-- 

INSERT INTO `statuses` (`status_id`, `user_id`, `sender_id`, `date`, `rate`, `comments`, `status`) VALUES 
(15, 11, 22, '2014-05-11 21:22:00', 0, 0, 'This is pretty damn cool! '), 
(16, 11, 91, '2014-05-11 21:22:35', 0, 0, 'LOL how did you do this shit man?? Alll you gotta do now is add the comments ;) and likes! '), 
(14, 11, 22, '2014-05-11 21:21:35', 0, 0, 'Hey budddy how are ya ? '), 
(13, 11, 11, '2014-05-11 21:18:10', 0, 0, 'eerer'), 
(11, 11, 11, '2014-05-11 21:10:33', 0, 0, 'Ho Ho!'), 
(10, 11, 11, '2014-05-11 21:10:27', 0, 0, 'Hey hey ! '), 
(28, 11, 11, '2014-05-12 00:47:02', 0, 0, 'LOL just another quick test ;) '), 
(29, 22, 22, '2014-05-12 02:30:44', 0, 0, 'I should be able to delete this status... '), 
(20, 11, 11, '2014-05-11 21:30:17', 0, 0, 'LOL WINNER'), 
(21, 22, 11, '2014-05-11 23:31:18', 0, 0, 'Hey mate :D '), 

(19, 11, 11, '2014-05-11 21:24:47', 0, 0, 'Not bad eh guys? ;P '); 

評論表:

-- 
-- Table structure for table `comments` 
-- 

CREATE TABLE IF NOT EXISTS `comments` (
    `comment_id` int(100) NOT NULL AUTO_INCREMENT, 
    `status_id` int(25) NOT NULL, 
    `user_id` int(25) NOT NULL, 
    `sender_id` int(25) NOT NULL, 
    `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `rate` int(25) NOT NULL DEFAULT '0', 
    `comment` text COLLATE utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`comment_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ; 

-- 
-- Dumping data for table `comments` 
-- 

INSERT INTO `comments` (`comment_id`, `status_id`, `user_id`, `sender_id`, `date`, `rate`, `comment`) VALUES 
(1, 28, 11, 11, '2014-05-12 01:23:58', 0, 'Hmmm shall we see if we can get this working too!? Comments, here we come! '), 
(2, 28, 11, 22, '2014-05-12 02:55:33', 0, 'This is not aidans comment! another username should appear!'); 

用戶表:

CREATE TABLE IF NOT EXISTS `users` (
    `userid` int(25) NOT NULL AUTO_INCREMENT, 
    `first_name` varchar(25) COLLATE latin1_general_ci NOT NULL, 
    `middle_name` varchar(25) COLLATE latin1_general_ci NOT NULL, 
    `last_name` varchar(25) COLLATE latin1_general_ci NOT NULL, 
    `email_address` varchar(50) COLLATE latin1_general_ci NOT NULL, 
    `username` varchar(25) COLLATE latin1_general_ci NOT NULL, 
    `password` varchar(255) COLLATE latin1_general_ci NOT NULL, 
    `photo` varchar(50) COLLATE latin1_general_ci NOT NULL DEFAULT 'default.png', 
    `date1` varchar(25) COLLATE latin1_general_ci NOT NULL, 
    `date2` varchar(25) COLLATE latin1_general_ci NOT NULL, 
    `date3` varchar(25) COLLATE latin1_general_ci NOT NULL, 
    `birthplace` varchar(50) COLLATE latin1_general_ci NOT NULL, 
    `gender` varchar(25) COLLATE latin1_general_ci NOT NULL, 
    `about` varchar(250) COLLATE latin1_general_ci NOT NULL DEFAULT 'This information has not yet been updated.', 
    `user_level` enum('0','1','2','3','4') COLLATE latin1_general_ci NOT NULL DEFAULT '0', 
    `signup_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `last_login` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `activated` enum('0','1') COLLATE latin1_general_ci NOT NULL DEFAULT '0', 
    `location` varchar(30) COLLATE latin1_general_ci NOT NULL, 
    `premium` enum('0','1') COLLATE latin1_general_ci NOT NULL DEFAULT '0', 
    `blocked` varchar(5) COLLATE latin1_general_ci NOT NULL DEFAULT '0', 
    `fr_alert` varchar(6) COLLATE latin1_general_ci NOT NULL DEFAULT '0', 
    `um_alert` varchar(6) COLLATE latin1_general_ci NOT NULL DEFAULT '0', 
    PRIMARY KEY (`userid`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Membership Information' AUTO_INCREMENT=92 ; 
+0

你想只有有評論的帖子出現嗎?或者你想要有一些沒有評論? –

+0

我們需要查看您當前的查詢 – kimbarcelona

+0

我希望所有帖子都可以顯示帶有或不帶註釋。 – Jessiikah1992

回答

1

大概是這樣的?不完全確定你的數據是什麼樣的,所以它可能是錯誤的..加入發送評論和狀態ID的用戶,然後通過評論排序DATE desc然後狀態DATE desc因此它會提供最多的評論最近的帖子

SELECT -- added alias to your tables so its easier to read 
    s.status_id, 
    s.user_id, 
    s.sender_id, 
    s.date, 
    s.rate, 
    s.comments as s_comment, 
    s.status, 
    u.userid as u_id, 
    u.username as u_name, 
    u.first_name as u_first, 
    u.last_name as u_last, 
    u.photo as u_photo, 
    o.userid as o_id, 
    o.username as o_name, 
    o.first_name as o_first, 
    o.last_name as o_last, 
    o.photo as o_photo, 
    c.comment as c_comment 
FROM statuses s 
LEFT JOIN users u ON s.sender_id=u.userid 
LEFT JOIN comments c ON c.user_id = u.userid AND c.status_id = s.status_id 
LEFT JOIN users o on o.userid = c.sender_id 
WHERE s.user_id = '{$profile_data['userid']}' 
ORDER BY c.date DESC, s.date DESC 

給它一個嘗試,讓我知道什麼是任何問題..同樣,如果你可以編輯你的文章幾行的實際數據,如一些評論與幾個職位和地位,像一個虛擬用戶,我可以測試這個我自己,並澄清(將加載更容易)

+0

我收到一個錯誤,提示「不是唯一表/別名:'o'」這是因爲用戶o與'u'相同嗎? – Jessiikah1992

+0

再次嘗試每個表名的別名..我認爲它試圖結合像列(具有相同的列名) –

+0

它的工作除了它顯示每個人的個人資料上的所有職位?哪裏的評論都是針對這一個地位的,它的地位是兩次展示,每個評論都有不同的評論? – Jessiikah1992

1

這個答案稍微改變了一些表格結構,但其背後的核心思想是相同的。

使用我這裏創建http://sqlfiddle.com/#!2/c1fa6e/1

因此,基本上可以INNER JOIN(兩個表中的結果)的status表,對錶users兩次,並使用AS運營商爲他們創造別名的例子。這可讓您加入用戶,獲取個人資料所有者信息以及發佈該狀態的人的信息。 (下面的例子僅顯示了在個人所有者#1發佈狀態)

SELECT status.status_id, 
    profile_owner.username AS owner, 
    status_sender.username AS sender 
    FROM status 
    INNER JOIN users AS profile_owner 
    ON profile_owner.user_id = status.user_id 
    INNER JOIN users AS status_sender 
    ON status_sender.user_id = status.sender_id 
    WHERE profile_owner.user_id = 1; 

結果

STATUS_ID OWNER  SENDER 
1   User 1 User 2 
2   User 1 User 4 

然後,爲了添加註釋,你LEFT JOIN意見表(有可能並不總是意見,這種情況下,這些領域將是無效的),那麼得到的評註的用戶的詳細信息,我們INNER JOIN用戶再次表,對評論者的ID來獲得他們的詳細資料

SELECT status.status_id, 
    profile_owner.username AS owner, 
    status_sender.username AS sender, 
    status.status, 
    status_comment.comment_body, 
    commentor.username AS commentor 
    FROM status 
    INNER JOIN users AS profile_owner 
    ON profile_owner.user_id = status.user_id 
    INNER JOIN users AS status_sender 
    ON status_sender.user_id = status.sender_id 
    LEFT JOIN status_comment 
    ON status.status_id = status_comment.status_id 
    LEFT JOIN users AS commentor 
    ON status_comment.commentor_id = commentor.user_id 
    WHERE profile_owner.user_id = 1; 

給這些結果

STATUS_ID OWNER SENDER STATUS       COMMENT_BODY       COMMENTOR 
1   User 1 User 2 Test status on User1 by User2 User3 commenting on status by User2. User 3 
1   User 1 User 2 Test status on User1 by User2 User4 commenting on status by User2. User 4 
2   User 1 User 4 Test status on User1 by User4 (null) (null) 

這說明用戶1有2個狀態張貼在他們的個人資料,一個是由用戶2和有2個評論,另一個是由用戶4,有沒有意見。然後你可以用PHP解析這些結果並適當地顯示。

一旦你分解它,它是相當簡單的。

您可以view it all in action here and play with it yourself on sqlfiddle

更新:使用您編輯到您的文章,I have created an SQLFiddle數據庫信息,以試圖向您展示如何將其應用到現有的數據庫。

這很簡單,你只是好像把我的例子翻譯成你的代碼有點不對。以下是正確的查詢,並更改了適當的表名。

SELECT statuses.status_id, 
    profile_owner.first_name AS owner, 
    status_sender.first_name AS sender, 
    statuses.status, 
    comments.comment, 
    commentor.first_name AS commentor 
    FROM statuses 
    INNER JOIN users AS profile_owner 
    ON profile_owner.userid = statuses.user_id 
    INNER JOIN users AS status_sender 
    ON status_sender.userid = statuses.sender_id 
    LEFT JOIN comments 
    ON statuses.status_id = comments.status_id 
    LEFT JOIN users AS commentor 
    ON comments.sender_id = commentor.userid 
    WHERE profile_owner.userid = 11; 

這會拉取用戶ID爲11的個人資料上發佈的所有狀態。它還會提取這些狀態可能具有的任何評論。

**請注意...... **我不確定爲什麼要在comments表中存儲sender_iduser_id。您的評論已經與某個狀態相關聯,因此您知道其針對的是哪些人,只需要存儲發表評論的人。

嘗試將每個實體看作自己的對象。

你有3個對象

  • 用戶
  • 狀態
  • 評論

一個狀態都有誰它的發佈對(status.user_id)用戶,誰貼吧的用戶(status.sender_id )。

評論具有針對(comments.status_id)發佈的狀態以及發佈該評論的用戶(comments.sender_id)。

無需再將status.userid存儲在評論表中,因爲您可以隨時加入它以獲取該信息。

+0

感謝您的幫助!這就是我期待的那種讓我更好地理解連接表的方式!儘管在我最初的查詢中,我有從查詢中列出的變量(即:statuses.status_id,statuses.user_id,users.userid,users.photo等...),我會在哪裏將它合併到查詢中以使用變量回作爲「$狀態['照片']」或什麼? – Jessiikah1992

+0

在'SELECT'語句中,您可以定義您希望返回哪些列作爲查詢的一部分。在我們的例子中,雖然因爲我們有名字相同的字段(比如'username's),我已經使用了「AS」別名操作符來重新命名結果集中的那一列。因此,要爲配置文件用戶提供「照片」列,可以在您的選擇中添加如下內容:'profile_owner.photo AS profile_owner_photo',然後您可以使用'$ status ['profile_owner_photo']' –

+0

我剛剛測試過並有一個錯誤說「表'u634727706_bc.status_comment'不存在」不知道爲什麼,因爲他們都存在? – Jessiikah1992