CASE切换内连接表mysql

时间:2015-03-29 01:19:18

标签: mysql sql case

所以我有一个包含其他三个表的编辑的表,我需要根据值ce.entity_id切换内连接。是的,我可以对这个edits content_edits表进行规范化,但这看起来要容易得多。

        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

真诚地感谢您的帮助。非常感谢。

2 个答案:

答案 0 :(得分:2)

联接不能依赖于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;

http://sqlfiddle.com/#!9/2b984/1

答案 1 :(得分:0)

CASE WHEN函数可用于有条件地过滤记录但不构建记录集。具体来说,这意味着它可以在SELECTWHERE和甚至JOIN ON子句中使用。但是,它不能依赖于动态标准选择要加入的表。

查询优化器的第一个任务是读取FROMJOIN行,然后用于过滤,聚合,选择和/或订购记录。

或者,您可以使用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;