简体   繁体   English

MySQL查询排序和合并两个表

[英]Mysql query to sort and merge two tables

I have two tables in my mysql database called forum_topics and forum_replies. 我的mysql数据库中有两个表,分别称为forum_topics和forum_replies。 I am looking to have an overview of the latest posts / replies added, like this: http://prntscr.com/6ixtz4 我希望概述添加的最新帖子/回复,例如: http : //prntscr.com/6ixtz4

to do so, i need a way to makee it sort by the time in both tables, and if a forum_reply is in the result it need to get its topic from the forum_topics. 为此,我需要一种方法使它在两个表中按时间排序,如果结果是forum_reply,则需要从forum_topics中获取其主题。

How can i make this work? 我该如何工作? I have no mysql query to referer to to make this work. 我没有要查询的mysql查询以使其工作。

    CREATE TABLE IF NOT EXISTS `forum_topics` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `topic` varchar(255) NOT NULL,
  `thread` varchar(255) NOT NULL,
  `level` int(2) NOT NULL DEFAULT '0',
  `creator` int(255) NOT NULL,
  `time` varchar(255) NOT NULL,
  `innlegg` text NOT NULL,
  `ip` varchar(255) NOT NULL,
  `locked` enum('yes','no') NOT NULL DEFAULT 'no',
  `deleted` enum('yes','no') NOT NULL DEFAULT 'no',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


CREATE TABLE IF NOT EXISTS `forum_replies` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `topicid` int(255) NOT NULL,
  `userid` int(255) NOT NULL,
  `time` int(255) NOT NULL,
  `reply` text NOT NULL,
  `deleted` enum('yes','no') NOT NULL DEFAULT 'no',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

exampledata: exampledata:

 in table forum_topics

id |topic| time|
1  | test | 10
2  |  test2 | 29



   in table forum replies:
id | topicid | time |

1   |  1   | 18
2   |  2   | 28

As in this example i would like the outcome to be sorted something like this: 在此示例中,我希望对结果进行如下排序:

10
18
28
29

if forum_replies is one of the results it would need some data from forum_topics, but if forum_topics is one of the results it wouldnt need any data from the forum_replies. 如果forum_replies是结果之一,则需要来自forum_topics的一些数据,但是如果forum_topics是结果之一,则它不需要来自forum_replies的任何数据。

The topicid is the id from the forum_topics of the topic that the reply is a response to. topicid是来自该回复的主题的forum_topics的ID。

So in the end i want to create the example output as in the image. 所以最后我想创建示例输出,如图所示。

EDIT: it would be a result containing in following order: 10 18 28 29 编辑:这将是一个包含以下顺序的结果:10 18 28 29

By loading the data in the example data sets, this will get the time from both tables, but sort the time from both as if they were in one table. 通过将数据加载到示例数据集中,这将从两个表中获取时间,但是从两个表中对时间进行排序,就好像它们在一个表中一样。 The below query gets the results, and then orders them in order of time. 下面的查询获取结果,然后按时间顺序对其进行排序。 Since both tables have the same structure, you can "union" them together as if they were one table and sort by time using "order_by" 由于两个表具有相同的结构,因此您可以将它们“统一”为一个表,并使用“ order_by”按时间排序

select time
from forum_topics
UNION
select time
from forum_replies
order by time

Note: I was able to load both tables in the above request but without the last line on each. 注意:我能够在上述请求中加载两个表,但每个表都没有最后一行。 In other words I trimmed the part referring to the engine, and then just created the tables, and then entered the data provided.. 换句话说,我修剪了引用引擎的零件,然后创建表,然后输入提供的数据。

I'm sorry I have to say the table structure of a forum post is incorrect. 对不起,我不得不说论坛帖子的表结构不正确。 In your case, you will be very hard to sort a post based on topic and reply. 就您而言,您将很难根据主题和回复对帖子进行排序。 I would suggest a table structure like this: 我建议这样的表结构:

CREATE TABLE IF NOT EXISTS `forum_topics` (
  `tid` int(255) NOT NULL AUTO_INCREMENT, // Topic ID
  `tsubject` varchar(255) NOT NULL, // Topic Subject
  `level` int(2) NOT NULL DEFAULT '0',
  `creator` int(255) NOT NULL,
  `innlegg` text NOT NULL, // I don't know what this means ...
  `locked` enum('yes','no') NOT NULL DEFAULT 'no',
  `deleted` enum('yes','no') NOT NULL DEFAULT 'no',
  PRIMARY KEY (`tid`),
  UNIQUE KEY `id` (`tid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5;

CREATE TABLE IF NOT EXISTS `forum_posts` (
  `pid` int(255) NOT NULL AUTO_INCREMENT,
  `tid` int(255) NOT NULL,
  `userid` int(255) NOT NULL,
  `time` int(255) NOT NULL,
  `pcontent` text NOT NULL, // Post Content
  `isauthorpost` int(1) NOT NULL, // Optional: If this is the first post of the topic
  `deleted` enum('yes','no') NOT NULL DEFAULT 'no',
  PRIMARY KEY (`pid`),
  UNIQUE KEY `id` (`pid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

So now you will have a topic id to follow by the first. 因此,现在您将拥有第一个跟随的主题ID。 And you can just search for all replies with ORDER BY pid , for the topic details, you can use LEFT JOIN forum_topics or INNER JOIN to finish. 您可以使用ORDER BY pid搜索所有答复,以获取主题详细信息,可以使用LEFT JOIN forum_topicsINNER JOIN完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM