[英]MYSQL customizable join and select
i want to use JOIN categories c ON c.id = i.category
if post_type
is 1 and i want to have c.title AS category_name,
in SELECT
otherwise JOIN categories c ON c.id = i.category
and c.title AS category_name,
must be not worked in query , i'm using case for join but my sql command is not correct. 如果post_type
为1,我想使用JOIN categories c ON c.id = i.category
,并且我想使用c.title AS category_name,
否则在SELECT
JOIN categories c ON c.id = i.category
和c.title AS category_name,
不能在查询中使用,我正在使用case进行连接,但是我的sql命令不正确。 please help me. 请帮我。 in all description my quastion means is how to change this below command to if post_type
is 1 join must be not work 在所有描述中,我的解决方法是如何将以下命令更改为post_type
为1的post_type
必须不起作用
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
I would always do the LEFT JOIN
and put a condition on the column itself: 我将始终执行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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.