是否可以做这样的事情:
SELECT
p.*, u.*
FROM
posts AS p
IF(p.status = 1)
LEFT JOIN users AS u
ON u.id = p.user_id
ELSE
LEFT JOIN pusers AS u
ON u.id = p.user_id
WHERE p.id = 10 ;
根据帖子状态为真/假加入 users/pusers 表
最佳答案
不,但你可以这样做:
SELECT p.*,
(case when p.status = 1 then u.col1 else pu.col1 end) as col1
FROM posts p LEFT JOIN
users u
ON u.id = p.user_id and p.status = 1 LEFT JOIN
pusers AS pu
ON pu.id = p.user_id and p.status <> 1
WHERE p.id = 10 ;
换句话说,您可以连接两个表并根据条件使用表中的值。
关于MySql 基于字段值连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31863731/