[英]Mysql - How to display column from 2nd table based on values from 2 columns of 1st table?
I have two tables, measure:我有两个表,测量:
id currentgroup nextgroup
1 1 2
2 2 5
3 3 24
4 99 NULL
description:描述:
id desc
1 First Level
2 Second Level
3 Third Level
4 Supervisor
5 Team Leader
.
.
99 CEO
and I would like to have this output我想要这个输出
id currentgroup nextgroup
1 First Level Second Level
2 Second Level Team Leader
3 Third Level DB department
4 CEO
These are the queries I tried.这些是我尝试过的查询。 To display first two columns, this one is working:
要显示前两列,这一列正在工作:
SELECT id, description.desc
FROM measure
INNER JOIN description ON measure.currentgroup=description.desc
WHERE measure.id IN (1,2,3,4);
QUESTION: I'm unable to add the second column.问题:我无法添加第二列。 I don't know how to define description.desc for the second column (nextgrouplevel).
我不知道如何为第二列(nextgrouplevel)定义 description.desc。 I added second join condition, but how to show this second column 'nextgroup'?
我添加了第二个连接条件,但如何显示第二列“nextgroup”?
SELECT id, description.desc
FROM measure
INNER JOIN description ON measure.currentgroup=description.desc AND measure.nextgroup=description.desc
WHERE measure.id IN (1,2,3,4);
You can join twice, once for each group.您可以加入两次,每个组一次。 Since you have some measures without a next group, the second join should be a
left join
.由于您有一些没有下一组的度量,因此第二个连接应该是
left join
。
select
m.id,
d1.desc current_group,
d2.desc next_group
from
measure m
inner join description d1 on d1.id = m.current_group
left join description d2 on d2.id = m.next_group
where m.id in (1, 2, 3, 4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.