Skip to content
Advertisement

elect data from related tables with duplicate columns

I have 3 tables.

news

id  title   description

rubrics

id  title   parent_id

parent_id is key for id parent in table rubrics.

And “news_rubriks”

id  news_id rubric_id

How I can select news which apply to rubrics.id and rubrics.parent_id ?

Advertisement

Answer

If I understand correctly, you want to apply a filter to both the rubrik id and the parent. Say if you are searching for 1, then you want 1 in either column.

If so:

select n.*
from news n join
     news_rubriks nr
     on nr.news_id = n.id left join
     rubrik r
     on nr.rubrik_id = r.id
where ? in (r.id, r.parent_id)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement