MySQL CASE LEFT JOINS 返回 NULL
MySQL CASE LEFT JOINS returning NULL
我有一个带有 CASE 语句的查询,该语句确定变量 object_name
此变量派生自 x_ambitions
table 或 x_trybes
table.下面的查询被组合在一起,这样我就可以通过执行一个 SQL 查询来保持简单。
我将 SELECT 语句分成两部分,以便您更好地理解。下面的两个查询。工作并从数据库中提取正确的 object_name。
我在组合这两个查询时遇到的问题是 'new_join_ambition','new_created_ambition','new_liked_ambition'
object_name returns NULL 在 LEFT JOIN
.
在组合查询中,如果我将案例:'new_join_ambition','new_created_ambition','new_liked_ambition'
置于 'new_join_trybe','new_created_trybe','new_liked_trybe'
案例之上。相反的情况发生了。 trybe 行 return NULL
。
两个SQL查询:
A:(检索对象 A)
SELECT
s.id,
s.object_id,
s.type,
s.postee_id,
s.user_id,
s.text,
s.registered,
CONCAT(u.x_first_name,' ',u.x_last_name) AS postee_name,
ui.image_id AS postee_image_id,
CASE s.type
WHEN 'new_join_ambition'
OR 'new_created_ambition'
OR 'new_liked_ambition'
THEN a.name
ELSE 'a'
END AS object_name
FROM
x_share s
LEFT JOIN
x_user u ON u.id = s.postee_id
LEFT JOIN
x_user_images ui ON ui.user_id = s.postee_id
LEFT JOIN
x_ambitions a ON s.type IN ('new_join_ambition', 'new_created_ambition', 'new_liked_ambition') AND s.object_id = a.id
LEFT JOIN
x_ambition_invites ai ON s.type IN ('new_join_ambition') AND s.object_id = ai.ambition_id AND s.postee_id = ai.to
LEFT JOIN
x_ambition_likes al ON s.type IN ('new_liked_ambition') AND s.object_id = al.ambition_id AND s.postee_id = al.profile_id
LEFT JOIN
x_ambition_owner aoo ON s.type IN ('new_created_ambition') AND s.object_id = aoo.ambition_id
WHERE
s.user_id = '%s'
ORDER BY
s.registered DESC
B:(检索对象 B)
SELECT
s.id,
s.object_id,
s.type,
s.postee_id,
s.user_id,
s.text,
s.registered,
CONCAT(u.x_first_name,' ',u.x_last_name) AS postee_name,
ui.image_id AS postee_image_id,
CASE s.type
WHEN 'new_join_trybe'
OR 'new_created_trybe'
OR 'new_liked_trybe'
THEN t.name
ELSE 'a'
END AS object_name
FROM
x_share s
LEFT JOIN
x_user u ON u.id = s.postee_id
LEFT JOIN
x_user_images ui ON ui.user_id = s.postee_id
LEFT JOIN
x_trybes t ON s.type IN ('new_join_trybe', 'new_created_trybe', 'new_liked_trybe') AND s.object_id = t.id
LEFT JOIN
x_trybe_invites ti ON s.type IN ('new_join_trybe') AND s.object_id = ti.trybe_id AND s.postee_id = ti.to
LEFT JOIN
x_trybes_likes tl ON s.type IN ('new_liked_trybe') AND s.object_id = tl.trybe_id AND s.postee_id = tl.profile_id
LEFT JOIN
x_trybe_owner too ON s.type IN ('new_created_trybe') AND s.object_id = too.trybe_id
WHERE
s.user_id = '%s'
ORDER BY
s.registered DESC
我已经 运行 这两个查询并捕获了两个查询结果的图像。
设置A:
B组:
如何在没有 object_name returning NULL 的情况下将两者结合起来?如果您有任何问题,请使用评论,我会毫不犹豫地回复。
提前致谢
我不知道你查询的其余部分,但你的 case
陈述不正确。你有:
(CASE s.type
WHEN 'new_join_ambition' OR 'new_created_ambition' OR 'new_liked_ambition'
THEN a.name
ELSE 'a'
END) AS object_name
OR
s 最终将值视为数字,因此这等同于 WHEN 0 THEN . . .
。
你要的是这种形式的 case
:
(CASE WHEN s.type IN ('new_join_ambition', 'new_created_ambition', 'new_liked_ambition')
THEN a.name
ELSE 'a'
END) AS object_name
我有一个带有 CASE 语句的查询,该语句确定变量 object_name
此变量派生自 x_ambitions
table 或 x_trybes
table.下面的查询被组合在一起,这样我就可以通过执行一个 SQL 查询来保持简单。
我将 SELECT 语句分成两部分,以便您更好地理解。下面的两个查询。工作并从数据库中提取正确的 object_name。
我在组合这两个查询时遇到的问题是 'new_join_ambition','new_created_ambition','new_liked_ambition'
object_name returns NULL 在 LEFT JOIN
.
在组合查询中,如果我将案例:'new_join_ambition','new_created_ambition','new_liked_ambition'
置于 'new_join_trybe','new_created_trybe','new_liked_trybe'
案例之上。相反的情况发生了。 trybe 行 return NULL
。
两个SQL查询:
A:(检索对象 A)
SELECT
s.id,
s.object_id,
s.type,
s.postee_id,
s.user_id,
s.text,
s.registered,
CONCAT(u.x_first_name,' ',u.x_last_name) AS postee_name,
ui.image_id AS postee_image_id,
CASE s.type
WHEN 'new_join_ambition'
OR 'new_created_ambition'
OR 'new_liked_ambition'
THEN a.name
ELSE 'a'
END AS object_name
FROM
x_share s
LEFT JOIN
x_user u ON u.id = s.postee_id
LEFT JOIN
x_user_images ui ON ui.user_id = s.postee_id
LEFT JOIN
x_ambitions a ON s.type IN ('new_join_ambition', 'new_created_ambition', 'new_liked_ambition') AND s.object_id = a.id
LEFT JOIN
x_ambition_invites ai ON s.type IN ('new_join_ambition') AND s.object_id = ai.ambition_id AND s.postee_id = ai.to
LEFT JOIN
x_ambition_likes al ON s.type IN ('new_liked_ambition') AND s.object_id = al.ambition_id AND s.postee_id = al.profile_id
LEFT JOIN
x_ambition_owner aoo ON s.type IN ('new_created_ambition') AND s.object_id = aoo.ambition_id
WHERE
s.user_id = '%s'
ORDER BY
s.registered DESC
B:(检索对象 B)
SELECT
s.id,
s.object_id,
s.type,
s.postee_id,
s.user_id,
s.text,
s.registered,
CONCAT(u.x_first_name,' ',u.x_last_name) AS postee_name,
ui.image_id AS postee_image_id,
CASE s.type
WHEN 'new_join_trybe'
OR 'new_created_trybe'
OR 'new_liked_trybe'
THEN t.name
ELSE 'a'
END AS object_name
FROM
x_share s
LEFT JOIN
x_user u ON u.id = s.postee_id
LEFT JOIN
x_user_images ui ON ui.user_id = s.postee_id
LEFT JOIN
x_trybes t ON s.type IN ('new_join_trybe', 'new_created_trybe', 'new_liked_trybe') AND s.object_id = t.id
LEFT JOIN
x_trybe_invites ti ON s.type IN ('new_join_trybe') AND s.object_id = ti.trybe_id AND s.postee_id = ti.to
LEFT JOIN
x_trybes_likes tl ON s.type IN ('new_liked_trybe') AND s.object_id = tl.trybe_id AND s.postee_id = tl.profile_id
LEFT JOIN
x_trybe_owner too ON s.type IN ('new_created_trybe') AND s.object_id = too.trybe_id
WHERE
s.user_id = '%s'
ORDER BY
s.registered DESC
我已经 运行 这两个查询并捕获了两个查询结果的图像。
设置A:
B组:
如何在没有 object_name returning NULL 的情况下将两者结合起来?如果您有任何问题,请使用评论,我会毫不犹豫地回复。
提前致谢
我不知道你查询的其余部分,但你的 case
陈述不正确。你有:
(CASE s.type
WHEN 'new_join_ambition' OR 'new_created_ambition' OR 'new_liked_ambition'
THEN a.name
ELSE 'a'
END) AS object_name
OR
s 最终将值视为数字,因此这等同于 WHEN 0 THEN . . .
。
你要的是这种形式的 case
:
(CASE WHEN s.type IN ('new_join_ambition', 'new_created_ambition', 'new_liked_ambition')
THEN a.name
ELSE 'a'
END) AS object_name