CASE 切换内部联接 table mysql
CASE switch inner join table mysql
所以我有一个 table,其中包含对其他三个 table 的编辑,我需要根据值 ce.entity_id 切换内部联接。是的,我本可以规范化此编辑 content_edits table,但这似乎容易得多。
SELECT ce.id, ce.file, ce.date_init, ce.content_type, ce.created_by_id, ce.status, ce.date_decide, u.username
FROM content_edits AS ce
INNER JOIN users AS u
ON ce.created_by_id=u.id
CASE ce.entity_type
WHEN ce.entity_type=0 THEN
INNER JOIN m_articles AS m
ON ce.entity_id=markers.id
WHEN 1 THEN
INNER JOIN groups AS g
ON ce.entity_id=groups.id
WHEN 3 THEN
INNER JOIN e_news AS e
ON ce.entity_id=events.id
WHERE ce.status=1
LIMIT 10
衷心感谢您的帮助。非常感谢。
CASE WHEN
函数可用于有条件地过滤记录但不能构造记录集。具体来说,这意味着它可以用在 SELECT
、WHERE
甚至 JOIN ON
子句中。但是,它不能根据动态标准选择要连接的表。
查询优化器的第一个任务是读取 FROM
和 JOIN
行,然后过滤、聚合、select、and/or 排序记录.
或者,您可以使用 UNION
查询。它可能是重复的,但它更易读,并且可以理解表的结构。并且根据ce_entity_id
,只有一个联合表会输出。
SELECT ce.id, ce.file, ce.date_init, ce.content_type,
ce.created_by_id, ce.status, ce.date_decide, u.username
FROM content_edits AS ce
INNER JOIN users AS u ON ce.created_by_id=u.id
INNER JOIN m_articles AS m ON ce.entity_id=markers.id
WHERE ce.entity_type = 0 AND ce.status=1
LIMIT 10;
UNION
SELECT ce.id, ce.file, ce.date_init, ce.content_type,
ce.created_by_id, ce.status, ce.date_decide, u.username
FROM content_edits AS ce
INNER JOIN users AS u ON ce.created_by_id=u.id
INNER JOIN groups AS g ON ce.entity_id=groups.id
WHERE ce.entity_type = 1 AND ce.status=1
LIMIT 10;
UNION
SELECT ce.id, ce.file, ce.date_init, ce.content_type,
ce.created_by_id, ce.status, ce.date_decide, u.username
FROM content_edits AS ce
INNER JOIN users AS u ON ce.created_by_id=u.id
INNER JOIN e_news AS e ON ce.entity_id=events.id
WHERE ce.entity_type = 3 AND ce.status=1
LIMIT 10;
连接不能依赖于 CASE
。您可以在内部查询中将所有三个表联合在一起,然后像下面的查询一样一起使用它们
SELECT ce.id, ce.file, ce.date_init, ce.content_type, ce.created_by_id, ce.status, ce.date_decide, u.username,S.details
FROM content_edits AS ce
INNER JOIN users AS u
ON ce.created_by_id=u.id
INNER JOIN (
/*add details required columns*/
select 0 as entity_type,a.id as entity_id,a.art_details as details
from m_articles AS a
union all
select 1 as entity_type,g.id as entity_id,g.grp_details as details
from groups AS g
union all
select 3 as entity_type,n.id as entity_id,n.n_details as details
from e_news AS n
)S
ON ce.entity_id=S.entity_id
AND ce.entity_type = S.entity_type
LIMIT 10;
所以我有一个 table,其中包含对其他三个 table 的编辑,我需要根据值 ce.entity_id 切换内部联接。是的,我本可以规范化此编辑 content_edits table,但这似乎容易得多。
SELECT ce.id, ce.file, ce.date_init, ce.content_type, ce.created_by_id, ce.status, ce.date_decide, u.username
FROM content_edits AS ce
INNER JOIN users AS u
ON ce.created_by_id=u.id
CASE ce.entity_type
WHEN ce.entity_type=0 THEN
INNER JOIN m_articles AS m
ON ce.entity_id=markers.id
WHEN 1 THEN
INNER JOIN groups AS g
ON ce.entity_id=groups.id
WHEN 3 THEN
INNER JOIN e_news AS e
ON ce.entity_id=events.id
WHERE ce.status=1
LIMIT 10
衷心感谢您的帮助。非常感谢。
CASE WHEN
函数可用于有条件地过滤记录但不能构造记录集。具体来说,这意味着它可以用在 SELECT
、WHERE
甚至 JOIN ON
子句中。但是,它不能根据动态标准选择要连接的表。
查询优化器的第一个任务是读取 FROM
和 JOIN
行,然后过滤、聚合、select、and/or 排序记录.
或者,您可以使用 UNION
查询。它可能是重复的,但它更易读,并且可以理解表的结构。并且根据ce_entity_id
,只有一个联合表会输出。
SELECT ce.id, ce.file, ce.date_init, ce.content_type,
ce.created_by_id, ce.status, ce.date_decide, u.username
FROM content_edits AS ce
INNER JOIN users AS u ON ce.created_by_id=u.id
INNER JOIN m_articles AS m ON ce.entity_id=markers.id
WHERE ce.entity_type = 0 AND ce.status=1
LIMIT 10;
UNION
SELECT ce.id, ce.file, ce.date_init, ce.content_type,
ce.created_by_id, ce.status, ce.date_decide, u.username
FROM content_edits AS ce
INNER JOIN users AS u ON ce.created_by_id=u.id
INNER JOIN groups AS g ON ce.entity_id=groups.id
WHERE ce.entity_type = 1 AND ce.status=1
LIMIT 10;
UNION
SELECT ce.id, ce.file, ce.date_init, ce.content_type,
ce.created_by_id, ce.status, ce.date_decide, u.username
FROM content_edits AS ce
INNER JOIN users AS u ON ce.created_by_id=u.id
INNER JOIN e_news AS e ON ce.entity_id=events.id
WHERE ce.entity_type = 3 AND ce.status=1
LIMIT 10;
连接不能依赖于 CASE
。您可以在内部查询中将所有三个表联合在一起,然后像下面的查询一样一起使用它们
SELECT ce.id, ce.file, ce.date_init, ce.content_type, ce.created_by_id, ce.status, ce.date_decide, u.username,S.details
FROM content_edits AS ce
INNER JOIN users AS u
ON ce.created_by_id=u.id
INNER JOIN (
/*add details required columns*/
select 0 as entity_type,a.id as entity_id,a.art_details as details
from m_articles AS a
union all
select 1 as entity_type,g.id as entity_id,g.grp_details as details
from groups AS g
union all
select 3 as entity_type,n.id as entity_id,n.n_details as details
from e_news AS n
)S
ON ce.entity_id=S.entity_id
AND ce.entity_type = S.entity_type
LIMIT 10;