whitebear:
私は、これらのテーブルを持っている一つのテキストが多く、多くのアイテムを持っています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
reast 1に持っているwriter
、または1つを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
少なくとも1で持っている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
どのように私はで2つのテーブルを連結するか、できますか?
ゴードン・リノフ:
用途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
) ;
あなたが一緒にテーブルを結合した後に、重複を削除する必要はありませんので、これは、はるかに優れた凝集を使用するよりもです。