简体   繁体   中英

Mysql query to sort and merge two tables

I have two tables in my mysql database called forum_topics and forum_replies. I am looking to have an overview of the latest posts / replies added, like this: 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.

How can i make this work? I have no mysql query to referer to to make this work.

    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:

 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.

The topicid is the id from the forum_topics of the topic that the reply is a response to.

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

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"

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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