Php MYSQL中的ORDER BY,条件使用函数的返回
我在寻找解决办法;查询是:Php MYSQL中的ORDER BY,条件使用函数的返回,php,mysql,phpmyadmin,sql-order-by,conditional-statements,Php,Mysql,Phpmyadmin,Sql Order By,Conditional Statements,我在寻找解决办法;查询是: SELECT MAX(forum_commenti.Data) AS MData, forum_post.id AS id, forum_post.Nome AS Nome, forum_post.Messaggio AS Messaggio, forum_post.Sezione AS Sezione, forum_post.Data AS Data, forum_post.Utente AS Utente, forum_post.Chius
SELECT MAX(forum_commenti.Data) AS MData, forum_post.id AS id, forum_post.Nome AS Nome, forum_post.Messaggio AS Messaggio, forum_post.Sezione AS Sezione, forum_post.Data AS Data, forum_post.Utente AS Utente, forum_post.Chiuso AS Chiuso, forum_post.Importante AS Importante
FROM forum_post LEFT OUTER JOIN forum_commenti ON forum_post.id = forum_commenti.Post
WHERE forum_post.Importante = 0
AND forum_post.Sezione = '".$_GET['id']."'
GROUP BY id, Nome, Messaggio, Sezione, Data, Utente, Chiuso, Importante
ORDER BY MData IS NOT NULL DESC, Data DESC
LIMIT $start, $per_page
这是一个论坛的查询;我试图显示数据的发布顺序。我想要的是:
- 如果一篇帖子没有回复,请使用他自己的日期来订购该帖子,否则请使用该帖子最后评论的日期。(其他信息无效)
ORDER BY MData IS NOT NULL DESC, Data ASC
它表示“不支持引用'MData'(引用组函数)”
我将该查询用于php函数
查询中涉及的表包括:
- 论坛帖子:包含论坛的所有帖子
- 论坛评论:包含所有帖子的所有回复,使用“帖子”作为论坛帖子的外键
谢谢大家的帮助。当使用子查询首先选择帖子以及最新评论的日期时,它会起作用。然后可以根据这两列上的表达式对结果进行排序。我使用了
IFNULL(上次发布日期、拥有日期)
来选择正确的日期,并在生效日期
列中提供该选项,用于教学目的:
SELECT *, IFNULL(last_post_date, own_date) AS effective_date
FROM (
SELECT
forum_post.post_id,
forum_post.date AS own_date,
MAX(forum_comment.date) AS last_post_date
FROM forum_post
LEFT OUTER JOIN forum_comment
ON (forum_post.post_id = forum_comment.post_id)
GROUP BY post_id
) posts
ORDER BY effective_date DESC;
请注意,我在回答中使用了稍微不同的表格设置,因为原始表格定义不可用,但它也适用于任何其他表格设置。以下是我的示例表:
CREATE TABLE `forum_post` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime DEFAULT NULL,
PRIMARY KEY (`post_id`)
);
CREATE TABLE `forum_comment` (
`comment_id` int(11) NOT NULL AUTO_INCREMENT,
`post_id` int(11) DEFAULT NULL,
`date` datetime DEFAULT NULL,
PRIMARY KEY (`comment_id`)
);
我使用以下示例数据进行测试:
论坛\u帖子
+---------+---------------------+
| post_id | date |
+---------+---------------------+
| 1 | 2014-12-01 00:00:00 |
| 2 | 2014-12-02 00:00:00 |
| 3 | 2014-12-02 00:00:00 |
| 4 | 2014-12-03 00:00:00 |
| 5 | 2014-12-03 00:00:00 |
| 6 | 2014-12-04 00:00:00 |
| 7 | 2014-12-06 00:00:00 |
| 8 | 2014-12-09 00:00:00 |
+---------+---------------------+
论坛评论:
+------------+---------+---------------------+
| comment_id | post_id | date |
+------------+---------+---------------------+
| 1 | 1 | 2014-12-01 00:00:00 |
| 2 | 1 | 2014-12-02 00:00:00 |
| 3 | 1 | 2014-12-03 00:00:00 |
| 4 | 2 | 2014-12-23 00:00:00 |
| 5 | 3 | 2014-12-09 00:00:00 |
| 6 | 3 | 2014-12-15 00:00:00 |
| 7 | 5 | 2014-12-15 00:00:00 |
| 8 | 7 | 2014-12-09 00:00:00 |
| 9 | 7 | 2014-12-11 00:00:00 |
+------------+---------+---------------------+
使用该数据,查询将返回以下结果:
+---------+---------------------+---------------------+---------------------+
| post_id | own_date | last_post_date | effective_date |
+---------+---------------------+---------------------+---------------------+
| 2 | 2014-12-02 00:00:00 | 2014-12-23 00:00:00 | 2014-12-23 00:00:00 |
| 3 | 2014-12-02 00:00:00 | 2014-12-15 00:00:00 | 2014-12-15 00:00:00 |
| 5 | 2014-12-03 00:00:00 | 2014-12-15 00:00:00 | 2014-12-15 00:00:00 |
| 7 | 2014-12-06 00:00:00 | 2014-12-11 00:00:00 | 2014-12-11 00:00:00 |
| 8 | 2014-12-09 00:00:00 | NULL | 2014-12-09 00:00:00 |
| 6 | 2014-12-04 00:00:00 | NULL | 2014-12-04 00:00:00 |
| 4 | 2014-12-03 00:00:00 | NULL | 2014-12-03 00:00:00 |
| 1 | 2014-12-01 00:00:00 | 2014-12-03 00:00:00 | 2014-12-03 00:00:00 |
+---------+---------------------+---------------------+---------------------+
您不能使用类似于Oracle Decode的命令吗?如果是现在,请选择最后一次发表评论的日期,否则请选择发布日期,提供相同的名称,您可以通过它订购。我可能正在寻找类似的内容。我尝试使用“按时间排序”,但警告仍然存在;我不能使用MData,这是SELECT(MAX)中使用的聚合函数的返回。它不是这样使用的。使用if()执行子查询,但加载具有相同名称的数据,而不是使用聚合,如从myTable order by data desc limit 1中选择数据,如果为null,则执行post,etcOkay我得到了它,并且我尝试了,我已经输入了所有查询的一个值,从选择到分组依据;在这之后,在新的选择中,我放置了与第一个相同的字段,但使用了新的别名,它不会给我相同的错误。在外部查询的orderby中,我插入了我在post中使用的条件(不是NULL…),它工作正常,但我没有得到正确的结果。我将编辑主帖子并显示我希望查询的结果。好的,显示实际输出和设计输出=)