0

在下面的 sql 命令中,我想添加选项来选择哪些主题必须显示普通帖子或静态页面?

在设置表中,我有 default_front_page 来更改此选项。如果这个字段不是 0 sql commad 必须是 fetch static page

default_front_page是静态页面 ID,i.id 是主题 ID。

所有帖子(例如普通或静态页面)都放入目录表中,我可以使用 post_type 字段来猜测这篇文章是正常的还是静态的。我现在在这段代码中的问题是什么?

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,
     CASE (s.sortable)
        WHEN 3 THEN 100000000 - i.id 
        WHEN 2 THEN i.date_time
        WHEN 1 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 AND 
         CASE WHEN s.default_front_page <> 0 
              THEN i.id = s.default_front_page
         END 
  ORDER BY SortField                  
  LIMIT {$portalSettings['display_post_count']};
4

1 回答 1

0

没有必要CASEWHERE子句中使用。AND并且OR应该这样做:

WHERE 
     i.portal = '{$portal_id}'
     AND (
           (post_type = 4 
             AND date(NOW()) BETWEEN i.from_dateTime AND i.to_dateTime
           )
           OR post_type = 1
         )         
     AND i.t_status = 1 
     AND (s.default_front_page = 0 OR i.id = s.default_front_page)
于 2013-09-17T12:36:07.170 回答