mysql 加入条件
mysql join under condition
我有这个 table id_name:
+----+------+
| id | name |
+----+------+
| 1 | one |
| 2 | two |
| 3 | three|
...等等,还有另一个table ids
+----+-----+-----+-----+----
| id | aid | bid | cid | ...
+----+-----+-----+-----+----
| 1 | 1 | 2 | 3 | ...
| 2 | 2 | null| null| ...
| 3 | 2 | 3 | null| ...
我想 select 像这样:
+----+-----+-----+-----+-----+-----+-----+----
| id | aid |aname| bid |bname| cid |cname| ...
+----+-----+-----+-----+-----+-----+-----+----
| 1 | 1 | one | 2 | two | 3 |three| ...
| 2 | 2 | two | null| null| null| null| ...
| 3 | 2 | two | 3 |three| null| null| ...
基本上,如果 id 不为空,则将名称提取到相应的列,否则保持名称为空。
在我的项目中,我有 7 列 (a-g)。
我尝试了 7 级连接,基本上是这样的:
SELECT id,a.id AS aid,a.name AS aname,b.id AS bid,b.name AS bname, ...
FROM ids AS t
JOIN id_name AS a ON a.id=t.aid
JOIN id_name AS b ON b.id=t.bid
...
等但后来我注意到,如果 t.b 为空,则 b.id=null 始终为假,因此我不会 select 任何行。
我怎样才能做类似 IF b.id IS NULL THEN JOIN id_name AS b ON b.id=t.bid ELSE "keep bname null"?
我知道我可以创建一个临时 table 并进行 7 次更新。但是有没有一种方法可以使用单个 SELECT 来完成此操作?
谢谢!
您必须使用外部联接。例如
SELECT id,a.id AS aid,a.name AS aname,b.id AS bid,b.name AS bname, ...
FROM ids AS t
LEFT OUTER JOIN id_name AS a ON a.id=t.aid
LEFT OUTER JOIN id_name AS b ON b.id=t.bid
...
您也可以使用 cross join
获得结果,而无需多次加入相同的 table
select ids.id,
aid,
max(case when aid = id_name.id then id_name.name end) as aname,
bid,
max(case when bid = id_name.id then id_name.name end ) as bname
from ids
cross join id_name
group by ids.id, aid, bid
select
i.id,
a.id as aid,
a.name as aname,
b.id as bid,
b.name as bname,
c.id as cid,
c.name as cname
from ids i
left join id_name a
on a.id = i.aid
left join id_name b
on b.id = i.bid
left join id_name c
on c.id = i.cid
SQL Fiddle: http://www.sqlfiddle.com/#!2/3ea82/1
我有这个 table id_name:
+----+------+
| id | name |
+----+------+
| 1 | one |
| 2 | two |
| 3 | three|
...等等,还有另一个table ids
+----+-----+-----+-----+----
| id | aid | bid | cid | ...
+----+-----+-----+-----+----
| 1 | 1 | 2 | 3 | ...
| 2 | 2 | null| null| ...
| 3 | 2 | 3 | null| ...
我想 select 像这样:
+----+-----+-----+-----+-----+-----+-----+----
| id | aid |aname| bid |bname| cid |cname| ...
+----+-----+-----+-----+-----+-----+-----+----
| 1 | 1 | one | 2 | two | 3 |three| ...
| 2 | 2 | two | null| null| null| null| ...
| 3 | 2 | two | 3 |three| null| null| ...
基本上,如果 id 不为空,则将名称提取到相应的列,否则保持名称为空。
在我的项目中,我有 7 列 (a-g)。
我尝试了 7 级连接,基本上是这样的:
SELECT id,a.id AS aid,a.name AS aname,b.id AS bid,b.name AS bname, ...
FROM ids AS t
JOIN id_name AS a ON a.id=t.aid
JOIN id_name AS b ON b.id=t.bid
...
等但后来我注意到,如果 t.b 为空,则 b.id=null 始终为假,因此我不会 select 任何行。 我怎样才能做类似 IF b.id IS NULL THEN JOIN id_name AS b ON b.id=t.bid ELSE "keep bname null"?
我知道我可以创建一个临时 table 并进行 7 次更新。但是有没有一种方法可以使用单个 SELECT 来完成此操作?
谢谢!
您必须使用外部联接。例如
SELECT id,a.id AS aid,a.name AS aname,b.id AS bid,b.name AS bname, ...
FROM ids AS t
LEFT OUTER JOIN id_name AS a ON a.id=t.aid
LEFT OUTER JOIN id_name AS b ON b.id=t.bid
...
您也可以使用 cross join
获得结果,而无需多次加入相同的 table
select ids.id,
aid,
max(case when aid = id_name.id then id_name.name end) as aname,
bid,
max(case when bid = id_name.id then id_name.name end ) as bname
from ids
cross join id_name
group by ids.id, aid, bid
select
i.id,
a.id as aid,
a.name as aname,
b.id as bid,
b.name as bname,
c.id as cid,
c.name as cname
from ids i
left join id_name a
on a.id = i.aid
left join id_name b
on b.id = i.bid
left join id_name c
on c.id = i.cid
SQL Fiddle: http://www.sqlfiddle.com/#!2/3ea82/1