[英]SQL Join tables of elements that have two types
I have a table of objects that can have two types: 我有一个对象表,可以有两种类型:
objects: 对象:
id | name | type1 | type2 |
-------------------------
1 | bla | 5 | 7 |
and a table of types 和一个类型的表
types: 类型:
id | name |
-----------
5 | typ5 |
7 | typ7 |
How could I join them like the following? 我怎么能加入他们如下?
object.id | object.name | typename1 | typename2 |
------------------------------------------------
I tried the following, but I obtained two rows per object 我尝试了以下方法,但每个对象获得两行
SELECT objects.id,objetcs.name,types.name
from objects
INNER JOIN types
ON objects.type1=types._id OR objects.type2=types._id
You need two joins: 你需要两个连接:
SELECT o.id, o.name, t1.name as name1, t2.name as name2
from objects o LEFT OUTER JOIN
types t1
ON o.type1 = t1._id LEFT OUTER JOIN
types t2
on o.type2 = t2._id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.