2010-06-28 49 views
2

我遇到SQL查詢時遇到問題。連接SQL中的表

SELECT t.topicname, m. *, ms.avatar 
    FROM `messages` m 
    INNER JOIN topics t ON m.topicid = t.topicid 
    inner join users u on m.author=u.username 
    inner join misc ms on u.userid=ms.userid 
    ORDER BY postdate DESC LIMIT 5 

我想要做的就是從主題表topicname,一切從消息表和頭像從雜項表

我由topicid加入主題和郵件表 我可以加入通過messages.author和users.username和messgages和用戶表加入到其它表上我加入users.userid和misc.userid

問題是我沒有得到的結果我想

這q uery適用於加入了郵件和主題表,但我需要添加其它表,只有這樣,才能加入該是throught用戶表

SELECT t.topicname, m. * 
    FROM `messages` m 
    INNER JOIN topics t ON m.topicid = t.topicid 
    ORDER BY postdate DESC LIMIT 5 

這裏是表結構

CREATE TABLE `messages` (
    `messageid` int(6) NOT NULL auto_increment, 
    `boardid` int(2) NOT NULL default '0', 
    `topicid` int(4) NOT NULL default '0', 
    `message` text NOT NULL, 
    `author` varchar(255) NOT NULL default '', 
    `postdate` datetime default NULL, 
    PRIMARY KEY (`messageid`) 
) 

CREATE TABLE `misc` (
    `miscid` int(4) NOT NULL auto_increment, 
    `userid` int(4) NOT NULL default '0', 
    `profpic` varchar(100) NOT NULL default '', 
    `avatar` varchar(100) NOT NULL default '', 
    `signature` text NOT NULL, 
    `alerts` enum('y','n') NOT NULL default 'y', 
    PRIMARY KEY (`miscid`) 
) 

CREATE TABLE `topics` (
    `topicid` int(4) NOT NULL auto_increment, 
    `boardid` int(2) NOT NULL default '0', 
    `topicname` varchar(255) NOT NULL default '', 
    `author` varchar(255) NOT NULL default '', 
    `counter` int(5) NOT NULL default '0', 
    `sticky` char(1) NOT NULL default 'n', 
    `locked` char(1) NOT NULL default 'n', 
    PRIMARY KEY (`topicid`) 
) 

CREATE TABLE `users` (
    `userid` int(25) NOT NULL auto_increment, 
    `first_name` varchar(25) NOT NULL default '', 
    `last_name` varchar(25) NOT NULL default '', 
    `email` varchar(255) NOT NULL default '', 
    `username` varchar(25) NOT NULL default '', 
    PRIMARY KEY (`userid`) 
) 

這是隻有2個連接的查詢輸出,這是我期望得到的(只有我想有一個化身)

它不漂亮,但這是數據庫給我的

topicname   messageid  boardid   topicid   message   author  postdate 
Mauris Eu Neque Ipsum 36 4 8 Suspendisse nibh risus, porta at cursus sed, tinci... iTuneStinker 2010-04-08 20:31:39 
Aliquam Erat Volutpat 35 5 15 Donec volutpat ligula eu lorem pharetra a adipisci... AwsomeMoon 2010-04-07 21:58:20 
Ut Non Risus Elit 34 2 14 Etiam cursus, erat sed placerat fringilla, risus a... ScaryHair 2010-04-07 15:35:34 
Quisque Rutrum Mattis Sagittis 33 5 9 Etiam in elit sit amet nulla scelerisque ultricies... ScaryHair 2010-04-07 15:33:46 
Where Do I Start Trying To Organise A Festival 32 3 12 Morbi a neque aliquam nisl varius lobortis. Sed ve... ScaryHair 2010-04-07 13:27:40 

,這裏是使用該連接的所有表的查詢結果

topicname   messageid  boardid   topicid   message   author  postdate  avatar 
Forum Rules And Guidelines 2 1 1 FORUM RULES  AdRock 2009-04-11 23:05:18  avatar_17200.jpg 
Forum Rules And Guidelines 3 2 2 FORUM RULES. AdRock 2009-04-11 23:05:18  avatar_17200.jpg 
Forum Rules And Guidelines 4 3 3 FORUM RULES  AdRock 2009-04-11 23:05:18  avatar_17200.jpg 
Forum Rules And Guidelines 5 4 4 FORUM RULES  AdRock 2009-04-11 23:05:18  avatar_17200.jpg 
Forum Rules And Guidelines 6 5 5 FORUM RULES  AdRock 2009-04-11 23:05:18  avatar_17200.jpg 

第一quert得到最後的5所記錄的信息表,這就是我想要的,但我希望能夠得到與每條消息的作者有關的頭像

+1

您的查詢對我看起來不錯。請張貼你得到什麼,你想得到什麼。另請發佈'用戶'的結構。 – Quassnoi 2010-06-28 11:24:09

+0

你沒有得到預期的結果?你得到什麼? – Himadri 2010-06-28 11:36:06

+0

爲什麼avatar,profpic&signature在單獨的表中而不是在用戶表中?從你最後的回答中,頭像似乎在那裏'avatar_17200.jpg'?這是你在找什麼,因爲我似乎無法明白你爲什麼錯過了化身? – DrColossos 2010-06-28 11:46:59

回答

2

似乎你沒有填寫每條消息的用戶和頭像。

嘗試用OUTER JOINS代替INNER JOINS

SELECT t.topicname, m. *, ms.avatar 
FROM `messages` m 
JOIN topics t 
ON  t.topicid = m.topicid 
LEFT JOIN 
     users u 
ON  u.username = m.author 
LEFT JOIN 
     misc ms 
ON  ms.userid = u.userid 
ORDER BY 
     m.postdate DESC 
LIMIT 5 
+0

非常感謝 我想我有一天嘗試過,並且我得到了虛擬形象的NULL。我知道是什麼原因造成的,現在可以運行.....非常感謝 – AdRock 2010-06-28 13:19:48