mysql - 2 在一个中选择语句
问题描述
起初,很抱歉英语不是很好。我有下一个情况:我有 2 个 SQL 选择语句:
(
select
calldate, src, dst, disposition, uniqueid
from cdr
where
calldate>curdate()
and (length(cnum)>5 or cnum='5555' or cnum='8888')
and (length(dst)>5 or dst='1111')
group by uniqueid
)
和
(
select
calldate, src, dst, disposition, uniqueid
from cdr
where
calldate>curdate() and (length(cnum)>5 or cnum='5555' or cnum='8888')
and (length(dst)>5 or dst='1111')
and disposition='ANSWERED'
)
它们之间的唯一区别是第二条语句显示 disposition=ANSWERED 的所有行,同时第一条语句显示所有行
我想用一个条件加入这两个语句:如果在第一个语句中 disposition=ANSWERED 或如果在第一个语句中 disposition=NO ANSWER 但在第二个条件中 disposition=ANSWERED - 然后在我的最后一个语句中 disposition=ANSWERED (如果它在这两个陈述中都是 NO ANSWER 它是 NO ANSWER)。
我写了这个最后的声明:
select a.calldate as 'Время', case a.src
when '4952763333' then 'clinic '
else a.src
end
as 'From where',
case a.dst
when '1111' then 'clinic '
else a.dst
end
as 'Destination',
case a.disposition
when (a.disposition='ANSWERED' and b.disposition='ANSWERED') then 'ANSWERED'
when (a.disposition='NO ANSWER' and b.disposition='ANSWERED') then 'ANSWERED'
when (a.disposition<>'ANSWERED' and b.disposition<>'ANSWERED') then 'NO ANSWER'
end
as 'Answer',
case a.src
when '4958885555' then 'Outcoming'
else 'Incoming'
end
as 'Type Call'
from (
select
calldate, src, dst, disposition, uniqueid
from cdr
where
calldate>curdate()
and (length(cnum)>5 or cnum='5555' or cnum='8888')
and (length(dst)>5 or dst='1111')
group by uniqueid
) a
left join (
select
calldate, src, dst, disposition, uniqueid
from cdr
where
calldate>curdate()
and (length(cnum)>5 or cnum='5555' or cnum='8888')
and (length(dst)>5 or dst='1111')
and disposition='ANSWERED'
) b on a.uniqueid=b.uniqueid;
但是在最后的声明中,我有一个问题:它总是显示为“已回答”。我该如何解决这个问题,我认为这个问题靠近案例“a.disposition”。在这里我不明白 SQL 可以与 a.disposition 和 b.disposition 一起使用(可能是我没有正确制定)。
最后,我想补充一点,它是带有 Asterisk 数据库的词。我会很高兴得到任何答案。
解决方案
我的意思是你的case
陈述有误。因为b.disposition
可能是唯一的'ANSWERED'
,或者NULL
您可以像这样更改它:
...
case
when
(a.disposition='ANSWERED' and b.disposition is not null) then 'ANSWERED'
when
(a.disposition='NO ANSWER' and b.disposition is not null) then 'ANSWERED'
when (a.disposition<>'ANSWERED' and b.disposition is null) then 'NO ANSWER'
else 'UNKNOWN'
end as 'Answer',
...
推荐阅读
- python - 如何同时对一组数字进行排序
- javascript - 未捕获的类型错误:无法读取未定义的属性“名称”[antdesign - switch]
- android - 使用 Android <= 5.0 的命令打开或关闭移动数据
- javascript - 如何在 Angular 6 项目中使用 javascript npm?
- docker - 没有 -v /var/run/docker.sock:/var/run/docker.sock 无法连接到 Docker 守护进程
- angular - 无法路由到子组件Angular
- postgresql - 当异常引发先前的成功事务时回滚
- php - 如何解决 Laravel 中未定义的方法链接()?
- ios - 在视图中添加子视图,该视图是 Window 的子视图
- javascript - 使用javascript实现对文件管理器的搜索?