我遇到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
我想要做的是从主题表中获取主题名称,来自消息表的所有内容和来自misc表的头像
我通过topicid加入主题和消息表 我可以通过messages.author和users.username加入messgages和users表,并加入misc表我加入users.userid和misc.userid
问题是我没有得到我想要的结果
此查询适用于已加入的消息和主题表,但我需要添加misc表,并且只有通过users表才能加入
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条记录,这就是我想要的但我希望能够获得与每条消息的作者相关的头像
答案 0 :(得分:2)
似乎您没有为每封邮件填写用户和头像。
尝试将INNER JOINS
替换为OUTER 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