MYSQL可自定义加入和选择

时间:2013-06-16 06:49:40

标签: php mysql

如果JOIN categories c ON c.id = i.category为1,我希望使用post_type我希望c.title AS category_name,SELECT JOIN categories c ON c.id = i.categoryc.title AS category_name,post_type必须没有在查询中工作,我使用case加入但我的sql命令不正确。请帮我。在所有描述中,我的quastion意味着如何更改以下命令,如果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 是1加入必须不起作用

{{1}}

1 个答案:

答案 0 :(得分: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