首页 > 解决方案 > 通过 or 连接两个表

问题描述

我有这些表一个文本有很多很多项目writerline

ttext_obj桌子

test obj
1    text1
2    text2
3    text3
4    text4

text_obj_writers桌子

text writer
1    2
2    3
2    4

text_obj_line桌子

text line
1    2
4    3
1    4

所以,我想拿起text_obj其中至少有一个writer或一个的行line

现在我做了这样的代码。

text_obj至少有一个的idwrite

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

text_obj至少有一个的idline

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

但我想把or这些

1
2
4

如何通过 or 连接两个表?

标签: mysqlsql

解决方案


你可以使用exists

select o.*
from ttext_obj o
where 
    exists (select 1 from text_obj_writers writer w where w.obj_id = o.id)
    or exists (select 1 from text_obj_lines l where l.obj_id = o.id)

推荐阅读