Concatenate two tables by or

whitebear :

I have these tables One text has many-many items writer,line

ttext_obj table

test obj
1    text1
2    text2
3    text3
4    text4

text_obj_writers table

text writer
1    2
2    3
2    4

text_obj_line table

text line
1    2
4    3
1    4

So, I want to pick up the rows of text_obj which have at reast one writer or one line.

For now I made code like this .

The text_obj id which has at least one write

SELECT text.id  FROM `text_obj` text 
inner join text_obj_writers writer 
on writer.obj_id = text.id  group by text.id

//it returns
1
2

The text_obj id which have at least one line

SELECT text.id  FROM `text_obj` text 
inner join text_obj_lines line 
on line.obj_id = text.id  group by text.id

//it returns
1
4

But I want to take or of these

1
2
4

How can I concatenate two tables by or ?

Gordon Linoff :

Use exists:

select o.*
from text_obj o
where exists (select 1
              from text_obj_writers tow 
              where tow.obj_id = o.id
             ) or
      exists (select 1
              from text_obj_lines tol 
              where tol.obj_id = o.id
             ) ;

This is much better than using aggregation, because you do not need to remove duplicates after joining the tables together.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=371699&siteId=1