1

我想在 post_type 为 1 时使用 join,我使用 case 进行 join,但我的 sql 命令不正确。请帮我。

mysql:

SELECT SQL_CALC_FOUND_ROWS
                   i. * , 
                   c.title AS category_name, 
                   s.title AS status_title, 
                   i.thumb_image, 
                   CONCAT( u.name, ' ', u.family ) AS author
                FROM contents i
                CASE WHEN post_type = 1 then 
                     JOIN categories c ON c.id = i.category
                end
                JOIN users u ON u.id = i.posted_by
                JOIN status_topics s ON s.id = i.t_status
                WHERE i.id = 2
4

2 回答 2

0

为什么不使用 if 而不是 case when?

句法:

IF search_condition THEN statement_list
    [ELSEIF search_condition THEN statement_list] ...
    [ELSE statement_list]
END IF

更多信息在这里:http ://dev.mysql.com/doc/refman/5.0/en/if.html

于 2013-06-15T18:39:42.510 回答
0

尝试:

SELECT SQL_CALC_FOUND_ROWS
       i. * , 
       c.title AS category_name, 
       s.title AS status_title, 
       i.thumb_image, 
       CONCAT( u.name, ' ', u.family ) AS author
FROM contents i
LEFT JOIN categories c 
  ON i.category = CASE post_type WHEN 1 then c.id END 
JOIN users u ON u.id = i.posted_by
JOIN status_topics s ON s.id = i.t_status
WHERE i.id = 2
于 2013-06-15T18:51:47.257 回答