对查询中的sql结果进行排序

g6ll5ycj  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(294)

我有这样一个问题:

SELECT A.*, 
       B.surname, 
       B.name, 
       C.url_address, 
       (SELECT Concat(file_path, '/', file_name) AS image 
        FROM   psuploadedfiles B 
        WHERE  enum = 3 
               AND category = A.id_user 
        ORDER  BY number ASC 
        LIMIT  1) AS image2, 
       (SELECT Concat(file_path, '/', file_name) AS image 
        FROM   psuploadedfiles B 
        WHERE  enum = 3 
               AND category = A.post_id 
        ORDER  BY number ASC 
        LIMIT  1) AS image 
FROM   psposts A 
       LEFT JOIN psuserdetails B 
              ON B.id_user = A.id_user 
       LEFT JOIN psuser C 
              ON C.id_user = A.id_user 
WHERE  A.enable = '1' 
       AND ( A.id_user = 21 
              OR ( A.id_user = '7' 
                    OR A.id_user = '1' 
                    OR A.id_user = '5' ) ) 
ORDER  BY date DESC 
LIMIT  0, 25;

在psposts表中,我有一个subscriptions\u date列(包含yyyy-mm-dd格式的日期),其中subscriptions\u date是文章升级之前的日期。
如何对结果进行排序,使顶部的帖子包含订阅日期、当前日期或未来日期(即升职日期),而其他帖子则位于下方?

m1m5dgzv

m1m5dgzv1#

有点像。。。

ORDER BY 
    CASE WHEN A.subscriptions_date >= NOW() THEN 0 ELSE 1 END,
    date

第一个排序条件将数据分成两组,当前或将来的订阅放在第一位。在每个组中,数据按 date .

相关问题