在MySQL查询中使用Cases with子句

时间:2015-01-07 17:52:33

标签: mysql sql mysqli

我需要你的帮助,在下面,我已经提到了我想要实现的细节以及我尝试过的东西。我卡住的地方。

我需要实现的目标

实际上我需要根据where子句加入表,基本上我有一个feed结构表,类似于下面的,我只提到了必要的列,因为有大量的列是不必要的在这里显示

feed_id | type_id | user_id | timestamp

我正在使用以下查询来获取结果。

SELECT feed.*
FROM `phpfox_feed` feed
JOIN `phpfox_user` u ON u.user_id = feed.user_id
Left Join `phpfox_friend` f  ON feed.user_id = f.user_id AND f.friend_user_id = 441
Left Join `phpfox_app` apps  ON feed.app_id = apps.app_id
Where feed.feed_reference = 0 
      AND feed.type_id in ("forum","blog","event","news","video","pages_created")

type_id中显示结果,即:新闻,事件,群组,博客

所以我希望如果行有'feed_type' = news然后它与特定表连接,类似如果行有'feed_type'= group它与其他表连接,例如我提到下面的新闻和组的情况< / p>

案例新闻:

Join phpfox_news ON phpfox_news.feed_id = feed.feed_id
Join phpfox_news_cateogries 
            ON phpfox_news.cateogry_id = phpfox_news_cateogries.cateogry_id
Where phpfox_news_cateogries.category_name in ("sports","politics","party")

案例组:

Join phpfox_group ON phpfox_group.feed_id = feed.feed_id
Where phpfox_group.category_name in ("sports","politics","party")

我尝试了什么

我通过使用我在Stuck中提到的示例尝试了下面的内容,但它给了我一个错误

SELECT feed.*
FROM `phpfox_feed` feed
JOIN `phpfox_user` u ON u.user_id = feed.user_id
Left Join `phpfox_friend` f  ON feed.user_id = f.user_id AND f.friend_user_id = 441
Left Join `phpfox_app` apps  ON feed.app_id = apps.app_id
Where feed.feed_reference = 0 
     AND feed.type_id in ("forum","blog","event","news","video","pages_created") 
  AND               
Case                
   When feed.type_id = "news" 
    Then Join phpfox_news ON phpfox_news.feed_id = feed.feed_id
         Join phpfox_news_cateogries 
             ON phpfox_news.cateogry_id = phpfox_news_cateogries.cateogry_id
          Where phpfox_news_cateogries.category_name in ("sports","politics","party")

   When feed.type_id  = "group" 
    Then Join phpfox_group ON phpfox_group.feed_id = feed.feed_id
        Where phpfox_group.category_name in ("sports","politics","party")

End

我被困的地方

我发现的是sql查询中的一个案例,但我发现它只支持select或in join,如下面的示例所示,我按照上面的尝试进行了操作

    Select LNext.player As NextPlayer
    From lineups As L
    Left Join lineups As LNext
    On LNext.BattingOrder Between 11 And 20
        And LNext.BattingOrder  = Case
                                    When L.BattingOrder  = 19 Then 11
                                    Else L.BattingOrder  + 1
                                    End
    Where L.battingOrder Between 11 And 20

查询

1-这种查询可以和mysql中的'Cases'一起使用吗?  2-请指导我如何使用Case with where子句来实现我的结果

我真的需要像你这样的大师的帮助。

2 个答案:

答案 0 :(得分:0)

尝试加入表格,同时将feed.type_id的条件添加为合适的类型。

而不是:

When feed.type_id = "news" Then Join phpfox_news ON phpfox_news.feed_id = feed.feed_id

尝试:

Join phpfox_news ON phpfox_news.feed_id = feed.feed_id AND feed.type_id = "news"

因此,您尝试运行的查询应更改为:

SELECT feed.*
FROM `phpfox_feed` feed
JOIN `phpfox_user` u ON u.user_id = feed.user_id
Left Join `phpfox_friend` f  ON feed.user_id = f.user_id AND f.friend_user_id = 441
Left Join `phpfox_app` apps  ON feed.app_id = apps.app_id
Left Join phpfox_news ON phpfox_news.feed_id = feed.feed_id AND feed.type_id = "news"
Left Join phpfox_news_cateogries ON phpfox_news.cateogry_id = phpfox_news_cateogries.cateogry_id
          AND phpfox_news_cateogries.category_name in ("sports","politics","party")
Left Join phpfox_group ON phpfox_group.feed_id = feed.feed_id AND feed.type_id  = "group" AND  phpfox_group.category_name in ("sports","politics","party")
Where feed.feed_reference = 0 
     AND feed.type_id in ("forum","blog","event","news","video","pages_created") 

答案 1 :(得分:0)

根据@ hofan41提到的指南,我使用以下查询从两个不同的连接中获取结果

SELECT feed.*
FROM `phpfox_feed` feed
JOIN `phpfox_user` u ON u.user_id = feed.user_id
Left Join `phpfox_friend` f  ON feed.user_id = f.user_id AND f.friend_user_id = 441
Left Join `phpfox_app` apps  ON feed.app_id = apps.app_id

Left Join phpfox_news_items ON phpfox_news_items.item_id = feed.item_id AND feed.type_id = "news"

Left Join phpfox_news_feeds ON phpfox_news_feeds.feed_id = phpfox_news_items.feed_id

Left Join phpfox_news_categories ON phpfox_news_categories.category_id = phpfox_news_feeds.category_id

Left Join phpfox_pages ON phpfox_pages.page_id = feed.item_id AND feed.type_id = "pages_created"          
LEFT JOIN phpfox_pages_type ON phpfox_pages_type.type_id = phpfox_pages.type_id

Where feed.feed_reference = 0 
AND feed.type_id in ("forum","blog","event","news","video","pages_created") AND (phpfox_news_categories.category_name in ("Movies","music") OR phpfox_pages_type.name in ("Movies","music"))
ORDER BY `feed`.`item_id`  ASC