开发者

Help with sorting results from database (JOIN?) in php

开发者 https://www.devze.com 2023-03-15 16:31 出处:网络
Im upgrading my forum a bit, and want to change the way the topics is listed. My topics is stored in this table:

Im upgrading my forum a bit, and want to change the way the topics is listed.

My topics is stored in this table:

CREATE TABLE `forum_emner` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `type_forum` CHAR(9) NOT NULL,
    `gjengid` INT(10) UNSIGNED NULL DEFAULT '0',
    `sticky` TINYINT(1) UNSIGNED NULL DEFAULT '0',
    `emne` VARCHAR(255) NOT NULL,
    `innlegg` TEXT NOT NULL,
    `brukerid_starter` MEDIUMINT(8) UNSIGNED NOT NULL,
    `startet_dato` INT(10) UNSIGNED NOT NULL,
    `antall_lest` INT(10) UNSIGNED NULL DEFAULT '0',
    `antall_svar` INT(10) UNSIGNED NULL DEFAULT '0',
    PRIMARY KEY (`id`),
    INDEX `type_forum` (`type_forum`),
    INDEX `gjengid` (`gjengid`),
    INDEX `sticky` (`sticky`),
    INDEX `brukerid_starter` (`brukerid_starter`)
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT

Answers to topics is stored 开发者_StackOverflow社区in this table (emneid equals id in the forum_emner table):

CREATE TABLE `forum_svar` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `emneid` INT(10) UNSIGNED NOT NULL,
    `brukerid_av` MEDIUMINT(8) UNSIGNED NOT NULL,
    `innlegg` TEXT NOT NULL,
    `dato` INT(10) UNSIGNED NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `emneid` (`emneid`),
    INDEX `brukerid_av` (`brukerid_av`)
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
ROW_FORMAT=DEFAULT

dato is when the answer was posted (time() in php).

What i want: I want to sort the topics after the field, dato in forum_svar. The topic with the newest answer is on the top, and so on. But if a topic is created after the top topics last answer this topic should be on the top (how a forum actually works).

I've tried myself, but its not working as it should.

SELECT *, `forum_emner`.id AS UnikTradID FROM `forum_emner` 
LEFT JOIN `forum_svar` ON (`forum_emner`.id = `forum_svar`.emneid) 
WHERE `forum_emner`.type_forum = :type AND `forum_emner`.sticky = 0 
ORDER BY `forum_svar`.dato DESC LIMIT :p1, :p2

Any help? :)


A side note: you should use AS as I do bellow to make the query look more readable. And you shouldn't keep your MySQL field names in something other than English. What if you hire someone not speaking your language? :)

The first thing to do is to get the newest thread ids. We can do that with such query:

SELECT MAX(fs.id) AS LastThreadId FROM forum_svar AS fs GROUP BY fs.emneid ORDER BY LastThreadId

Now we need to join this result set with the threads to get full thread information. So we do it like this by putting the first query as a subquery here:

SELECT fs.* FROM forum_svar AS fs LEFT JOIN (SELECT MAX(fs.id) AS LastThreadId FROM forum_svar AS fs GROUP BY fs.emneid ORDER BY LastThreadId DESC) n ON (n.LastThreadId = fs.id) WHERE n.LastThreadId IS NOT NULL

This will now get you the newest threads. Enjoy! :)
P.S. Don't forget to accept the answer if it worked out!

0

精彩评论

暂无评论...
验证码 换一张
取 消