简体   繁体   English

使用多个内部联接过滤一个表与其他表

[英]Filtering one table with multiple inner joins against other tables

I have four tables, Photo, Event, News, Spot and Photo is the table i want to check for records with relations to other tables. 我有四张桌子,照片,事件,新闻,现场和照片是我想要检查与其他桌子有关系的记录的表格。

Photo has the following sructure: 照片有以下结构:

id
rel_model -> one of "news", "spot" and "event"
rel_id    -> id of the related record in rel_model table 
...

Tables other than Photo are constantly updated and some of the records ar deleted. Photo以外的表格会不断更新,并删除一些记录。 I want to filter the photos to get the records that are related to existing records on other tables. 我想过滤照片以获取与其他表上的现有记录相关的记录。

I tried the following 我尝试了以下内容

select 
    count(*)
from
    Photo
        inner join Event ON (rel_id = Event.id and rel_model="event") 
        inner join News ON (rel_id = News.id and rel_model="news")
        inner join Spot ON (rel_id = Spot.id and rel_model="spot"); 

but I get 0 results where trying it with only one inner join works for checking against single table 但我得到0结果,只有一个内连接尝试它来检查单个表

select 
    count(*)
from
    Photo
        inner join Event ON (rel_id = Event.id and rel_model="event") ;

I need to add some and-or logic inbetween inner joins, bit could not figure out how. 我需要在内连接之间添加一些和/或逻辑,位无法弄清楚如何。

How can I fetch the photos that still have unbroken relations to other tables? 如何获取与其他表格仍有不间断关系的照片?

you could use this query 你可以使用这个查询

select 
    count(*)
from Photo as P
where
    P.rel_model = "event" and P.rel_id in (select T.id from Event as T) or
    P.rel_model = "news" and P.rel_id in (select T.id from News as T) or
    P.rel_model = "spot" and P.rel_id in (select T.id from Spot as T)

If you want to change your query, you should use left outer join : 如果要更改查询,则应使用left outer join

select 
    count(*)
from Photo as P
    left outer join Event ON (rel_id = Event.id and rel_model="event") 
    left outer join News ON (rel_id = News.id and rel_model="news")
    left outer join Spot ON (rel_id = Spot.id and rel_model="spot")
where News.id is not null or Spot.id is not null or Event.id is not null

Your query return null rows because you trying to join same row with all three tables, but you join condition is matched only one, so other two inner joins eliminate you row. 您的查询返回空行,因为您尝试将所有三个表连接到同一行,但您的连接条件只匹配一个,因此其他两个内部连接将消除您的行。

You can do this using outer joins. 您可以使用外连接执行此操作。 With the inner joins you are losing a row when rel_id fails to match any of the three (and presumably, it matches only one of them, so you lose all rows). 使用内部联接时,当rel_id无法匹配三个中的任何一个时,您将丢失一行(并且可能,它只匹配其中一个,因此您将丢失所有行)。 Then, you need to count each one separately: 然后,您需要分别计算每一个:

select count(Event.id) + count(News.id) + count(Spot.id)
from Photo p left join
     Event
     ON p.rel_id = Event.id and rel_model="event" left join
     News
     ON p.rel_id = News.id and rel_model="news" left join
     Spot
     ON p.rel_id = Spot.id and rel_model="spot"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM