php – Mysql在case语句中使用DESC

在下面的sql命令中我想在WHEN 1那么i.id行之后使用DESC.我想如果sortable字段是1那么order by必须有这样的命令,
ORDER BY i.id DESC

MysqL

SELECT 
     sql_CALC_FOUND_ROWS i.*,c.title AS category_name,u.name,u.family,i.thumb_image,CONCAT( u.name,' ',u.family ) AS author,tumbnail_image_width,tumbnail_image_height
  FROM   contents i
  JOIN   categories c ON c.id = i.category
  JOIN   users u ON u.id = i.posted_by
  JOIN   settings s ON s.portal = i.portal
  WHERE 
         i.portal = '{$portal_id}'
         AND CASE WHEN post_type = 4
              THEN date(NOW()) BETWEEN i.from_dateTime AND i.to_dateTime 
         ELSE post_type = 1
         END
  AND i.t_status = 1
  ORDER BY
     CASE (SELECT sortable FROM settings)
        WHEN 1 THEN i.id 
        WHEN 2 THEN i.date_time
        WHEN 3 THEN i.order_display
     END                         
  LIMIT {$portalSettings['display_post_count']};";
可能会返回SELECT中的排序字段,然后按该命名字段排序: –
SELECT 
     sql_CALC_FOUND_ROWS i.*,tumbnail_image_height,CASE (s.sortable)
        WHEN 1 THEN 100000000 - i.id 
        WHEN 2 THEN i.date_time
        WHEN 3 THEN i.order_display
     END  AS SortField
  FROM   contents i
  JOIN   categories c ON c.id = i.category
  JOIN   users u ON u.id = i.posted_by
  JOIN   settings s ON s.portal = i.portal
  WHERE 
         i.portal = '{$portal_id}'
         AND CASE WHEN post_type = 4
              THEN date(NOW()) BETWEEN i.from_dateTime AND i.to_dateTime 
         ELSE post_type = 1
         END
  AND i.t_status = 1
  ORDER BY SortField                  
  LIMIT {$portalSettings['display_post_count']};";

请注意,您可能必须将字段强制转换为数据类型才能执行此操作.

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...