排序顺序错误(加入)

时间:2019-05-27 15:23:52

标签: php mysql sql-order-by

如何按“表#__kunena_topics”中最后一篇文章“ last_post_time”的日期对主题进行排序?

    $query ='SELECT cat.name as cat_name,
  cat.id as cat_id,
  top.id as top_id,
  top.subject as top_subject
FROM #__kunena_topics top
JOIN #__kunena_categories cat ON cat.id = top.category_id
WHERE cat.parent_id = '.$secid.' and cat.locked = 0 and top.locked = 0
AND (
  SELECT COUNT(*)
  FROM #__kunena_topics
  WHERE top.category_id = category_id
  AND top.id <= id
) <= 3';

我尝试过,但是不起作用:

 ..........
  SELECT COUNT(*)
  FROM #__kunena_topics
  WHERE top.category_id = category_id
  AND top.id <= id
) <= 3 ORDER BY top.last_post_time DESC'; 

结果

foreach ($this->items as $i => $object) : 
?>
    <?php   if($object->cat_id != $lastcategory) { ?>
    <div style="margin-top: 10px;border-bottom:3px solid black;">
    <?php echo "category:". $object->cat_id; ?>
    </div>
    <?php  } ?>
    <div style="margin-top: 1px;border-bottom:1px solid black;"> <?php
     echo "topic:". $object->top_subject;
      $lastcategory = $object->cat_id;
      ?>
      </div> <?php
                endforeach; ?>

现在显示

"CATEGORY 1"; "topic 3"; "topic 4"; "topic 5"; "CATEGORY 2"; "topic 55"; "topic 56"; "topic 57"; 

我想要

"CATEGORY 1"; "topic 5"; "topic 4"; "topic 3"; "CATEGORY 2"; "topic 57"; "topic 56"; "topic 55";

表格:#__kunena_topics

tabel: #__kunena_topics

结果:

result

0 个答案:

没有答案