0

我想使用JOIN categories c ON c.id = i.categoryif post_typeis 1 并且我想c.title AS category_name,SELECT其他情况下使用JOIN categories c ON c.id = i.category并且c.title AS category_name,不能在查询中工作,我使用 case 进行连接,但我的 sql 命令不正确。请帮我。在所有描述中,我的问题是如何将以下命令更改为 if post_typeis 1 join must not work

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
4

1 回答 1

0

我总是会这样做LEFT JOIN并对列本身设置一个条件:

SELECT SQL_CALC_FOUND_ROWS
   i. * , 
   IF(i.post_type = 1, c.title, NULL) 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 = c.id
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-16T06:55:01.827 回答