我正在尝试在子查询中使用主查询的结果单元格。
我有两张桌子
第一张桌子是电话
id | 名称 | 月 |
---|---|---|
151 | raj | Jan_2021 |
152 | danny | 2021 年 2 月 |
第二张桌子是ss
id | Jan_2021 | 2021 年 2 月 |
---|---|---|
151 | 2500 | 2200 |
152 | 1000 | 500 |
Mysql 查询 iam 使用
SELECT
name,
id,
month,
( select SOD.month
from ss
where Empid=SOD.eid
) as ava
FROM
telephone as SOD
join ss
on SOD.id=ss.Emp_id
但我没有得到我想要的确切结果。 请帮我从这张表中得到这个结果
id | 名称 | 月 | ava |
---|---|---|---|
151 | raj | Jan_2021 | 2500 |
152 | danny | 2021 年 2 月 | 500 |
答案 0 :(得分:0)
请检查小提琴中的答案: http://sqlfiddle.com/#!9/7c62c/7
select tel.id,tel.name,tel.month,
case
when tel.month='jan_2021' then ss.jan_2021
when tel.month='feb_2021' then ss.feb_2021
when tel.month='mar_2021' then ss.mar_2021
end
as monthly
from telephone tel,ss
where tel.id=ss.id
答案 1 :(得分:-1)
我会用 JOIN
和条件逻辑来解决这个问题:
select t.*
(case t.month when 'jan_2021' then ss.jan_2021
t.month when 'feb_2021' then ss.feb_2021
t.month when 'mar_2021' then ss.mar_2021
end) as monthly
from telephone t join
ss
on t.id = ss.id