如何使用 mysql & 连接 3 个表? php?
我有一个页面可以提取用户帖子、用户名、xbc/xlk 标签等,这是完美的...但是由于我是从 MyBB 公告板系统提取信息,所以它完全不同。回复时,人们可以通过简单的回复和更改来更改“主题”。
我不希望它显示更改后的主题标题,而只是显示该线程中所有帖子的原始标题。
默认情况下,它会回复“RE:线程标题”。他们可以轻松地编辑它,它将显示在“主题”单元格和“主题”单元格中。人们不会知道它发布在哪个线程中,因为他们在回复帖子时更改了线程。
所以我只想在他们回复时保留原始帖子标题。
有道理~??
表:mybb_users 字段:uid、用户名
表:mybb_userfields 字段:ufid
表:mybb_posts 字段:pid、tid、replyto、subject、ufid、username、uid、message
表:mybb_threads 字段:tid,fid,subject,uid,username,lastpost,lastposter,lastposteruid
我尝试了多个查询但没有成功:
$result = mysql_query("
SELECT * FROM mybb_users
LEFT JOIN (mybb_posts, mybb_userfields, mybb_threads)
ON (
mybb_userfields.ufid=mybb_posts.uid
AND mybb_threads.tid=mybb_posts.tid
AND mybb_users.uid=mybb_userfields.ufid
)
WHERE mybb_posts.fid=42");
$result = mysql_query("
SELECT * FROM mybb_users
LEFT JOIN (mybb_posts, mybb_userfields, mybb_threads)
ON (
mybb_userfields.ufid=mybb_posts.uid
AND mybb_threads.tid=mybb_posts.tid
AND mybb_users.uid=mybb_posts.uid
)
WHERE mybb_threads.fid=42");
$result = mysql_query("
SELECT * FROM mybb_posts
LEFT JOIN (mybb_userfields, mybb_threads)
ON (
mybb_userfields.ufid=mybb_posts.uid
AND mybb_threads.tid=mybb_posts.tid
)
WHERE mybb_posts.fid=42");
I have a page that pulls the users Post,username,xbc/xlk tags etc which is perfect... BUT since I am pulling information from a MyBB bulletin board system, its quite different. When replying, people are are allowed to change the "Thread Subject" by simplying replying and changing it.
I dont want it to SHOW the changed subject title, just the original title of all posts in that thread.
By default it repies with "RE:thread title". They can easily edit this and it will show up in the "Subject" cell & people wont know which thread it was posted in because they changed their thread to when replying to the post.
So I just want to keep the orginial thread title when they are replying.
Make sense~??
Tables:mybb_users
Fields:uid,username
Tables:mybb_userfields
Fields:ufid
Tables:mybb_posts
Fields:pid,tid,replyto,subject,ufid,username,uid,message
Tables:mybb_threads
Fields:tid,fid,subject,uid,username,lastpost,lastposter,lastposteruid
I haev tried multiple queries with no success:
$result = mysql_query("
SELECT * FROM mybb_users
LEFT JOIN (mybb_posts, mybb_userfields, mybb_threads)
ON (
mybb_userfields.ufid=mybb_posts.uid
AND mybb_threads.tid=mybb_posts.tid
AND mybb_users.uid=mybb_userfields.ufid
)
WHERE mybb_posts.fid=42");
$result = mysql_query("
SELECT * FROM mybb_users
LEFT JOIN (mybb_posts, mybb_userfields, mybb_threads)
ON (
mybb_userfields.ufid=mybb_posts.uid
AND mybb_threads.tid=mybb_posts.tid
AND mybb_users.uid=mybb_posts.uid
)
WHERE mybb_threads.fid=42");
$result = mysql_query("
SELECT * FROM mybb_posts
LEFT JOIN (mybb_userfields, mybb_threads)
ON (
mybb_userfields.ufid=mybb_posts.uid
AND mybb_threads.tid=mybb_posts.tid
)
WHERE mybb_posts.fid=42");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的语法不适合执行多个 LEFT JOIN 。每个连接都需要自己的
ON
子句。该查询应该给出您想要的结果。但它可能不是获取这些结果的最有效的查询。作为测试的一部分,检查 EXPLAIN 的输出,以确保它没有使用表扫描或类似的东西。
所有这些连接都需要是 LEFT JOIN 吗? LEFT JOIN 强制 MySQL 按指示的顺序连接表,而不是让查询优化器确定连接它们的最佳顺序。这就是为什么您可能需要小心查询执行计划。就查询输出而言,
JOIN
和LEFT JOIN
之间的主要区别在于,LEFT JOIN
结果集每行至少包含一行联接左侧的表的行,而如果联接右侧没有匹配项,则常规 JOIN 将不包含行。编辑:另外,你说“我不希望它显示更改后的主题标题,而只是显示该线程中所有帖子的原始标题。”这表明您只需要这些表中列的子集,在这种情况下
SELECT *
是不合适的。Your syntax isn't appropriate for carrying out multiple
LEFT JOIN
s. Each join needs its ownON
clause.This query should give the results you want. But it may not be the most efficient query for getting those results. Check the output of
EXPLAIN
as part of testing, to make sure it is not using table scans or anything like that.Do all of these joins need to be
LEFT JOIN
s?LEFT JOIN
forces MySQL to join the tables in the indicated order, rather than allowing the query optimiser to determine the best order in which to join them. That's why you might need to be careful about the query execution plan. The main difference betweenJOIN
andLEFT JOIN
as far as query output is concerned is thatLEFT JOIN
resultsets will contain at least one row for each row of the table on the left-hand side of the join, whereas a regularJOIN
will not contain a row if there aren't matches on the right-hand side of the join.Edit: Also, you say that "I don't want it to SHOW the changed subject title, just the original title of all posts in that thread." This suggests that you only want a subset of the columns from these tables, in which case
SELECT *
is inappropriate.