简体   繁体   English

Mysql - 如何根据第一个表的 2 列的值显示第二个表中的列?

[英]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.

相关问题 mySQL 如何从第一个表中选择不同的(1 列:pruduct_Type)并从第二个表中选择 2 列 - mySQL How to select distinct(1 column : pruduct_Type) from 1st table and select 2 columns from 2nd table 如何从第一个表中返回所有列,而从第二个表中只返回一列 - How to return all columns from 1st table and only 1 column from 2nd table 如何在同一选择语句中从第一张表的列值中选择第二张表的数据? - How to select data of 2nd table from column values of 1st table in same select statement? 如何将某些列数据从第一表移动到第二表。 并在单个查询中用不同的数据填充第二张表的其他列 - How to move certain column data from 1st table to 2nd table. And filling other columns of 2nd table with different data in a single query 在mySql join语句中显示第1个表的所有结果,但仅显示第2个表的第一个结果 - Show all results from 1st table but only 1st result from 2nd table in mySql join statement 从第2和第3个表中获取第1个表中匹配值的值 - Get the values from 2nd and 3rd table for the matched values in 1st table MySQL:按 2 列分组以接收来自第 1 列和第 2 列的所有值 - MySQL: grouping by 2 columns to receive all values from 1st and 2nd columns 如何从2个表中删除mysql记录,但是如果第2个表中没有记录仍从第1个表中删除呢? - How do I delete mysql records from 2 tables, but if no records in 2nd table still delete from 1st table? 从第一表中获取不在第二表mysql中的记录 - Fetch record from 1st table which are not in 2nd table mysql 是否可以使用PHP PDO从第一张表中的列从第二张表中获取数据? - Is it possible to get data from the 2nd table using column in 1st table using PHP PDO?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM