mysql - 通过 or 连接两个表
问题描述
我有这些表一个文本有很多很多项目writer
,line
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 连接两个表?
解决方案
你可以使用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)
推荐阅读
- android - 在 Kotlin 中可观察的房间用于单个数据库条目
- xml - 从 DateTime XQuery 中删除小时
- python - 雪花蟒蛇连接器;发出多个请求时出现错误 604
- cypress - Cypress.io:CLI --config-file & Cypress.env(),未定义
- java - hunspell 第三方库抛出运行时错误导致 JVM 致命错误
- python - Python + R 文件可以在语言之间共享 webdriver 会话吗?
- ios - 如果无法从 Firebase 实时数据库中检索数据,则尝试在 UITableView 上显示消息
- python - 使用第三方 haar 分类器时出现错误
- c# - 如何将 DataSet 转换为 XML 并返回 DataSet
- laravel - 如何从laravel中的表单中获取id