关于mysql:从另一个表中选择而不进行联接

Selecting from another table without a join

我在下面有一个有效的查询,但是我需要在我的select语句中为这些代理选择名字和姓氏,而没有任何实际值可以连接表。

我的另一个表是ambition.ambition_users,它具有first_name,last_name和扩展名。我有子查询正确提取扩展并将其视为id。但是,在我的主要选择中,我还要选择first_name和last_name。我似乎找不到通过简单的子查询执行此操作的方法,并且我没有任何要加入表的内容,除非我能够在ambition_users.extention上加入创建的id

在不影响当前查询的聚合的情况下实现拉名字和姓氏的最佳方法是什么?

这是工作查询:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
select
case
when callingpartyno       in (select extension from ambition.ambition_users)
then callingpartyno
when finallycalledpartyno in (select extension from ambition.ambition_users)
then finallycalledpartyno
 end as id

-- this is where i want to select first_name and last_name from ambition.ambition_users

, sum(duration) as total_talk_time_seconds
, round(sum(duration) / 60,2) as total_talk_time_minutes
, sum(if(legtype1 = 1,1,0)) as total_outbound
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed
, sum(if(legtype1 = 1, 1, 0)) +                   -- outbound calls
 sum(if(legtype1 = 2, 1, 0))  as total_calls
, now() as time_of_report
, curdate() as date_of_report
 from
  ambition.session a
  join ambition.callsummary b
  on a.notablecallid = b.notablecallid
 where
date(a.ts) >= curdate()
 and (
callingpartyno in (select extension from ambition.ambition_users
)
 or  finallycalledpartyno in (select extension from ambition.ambition_users
)
)
 group by
 id;


n


我没有在您的表之间建立关系,但是您可以使用以下逻辑在select语句中获取带有子查询的名字和姓氏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    select
    case
    when callingpartyno       in (select extension from ambition.ambition_users)
    then callingpartyno
    when finallycalledpartyno in (select extension from ambition.ambition_users)
    then finallycalledpartyno
     end as id

    -- this is where i want to select first_name and last_name from ambition.ambition_users

Max((select firstname from ambition.ambition_users as t1 where t1. Extension=b.callingpartyno)),
Max((select lastname from ambition.ambition_users as t1 where t1. Extension=b.callingpartyno)),

    , sum(duration) as total_talk_time_seconds
    , round(sum(duration) / 60,2) as total_talk_time_minutes
    , sum(if(legtype1 = 1,1,0)) as total_outbound
    , sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound
    , sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed
    , sum(if(legtype1 = 1, 1, 0)) +                   -- outbound calls
     sum(if(legtype1 = 2, 1, 0))  as total_calls
    , now() as time_of_report
    , curdate() as date_of_report
     from
      ambition.session a
      join ambition.callsummary b
      on a.notablecallid = b.notablecallid
     where
    date(a.ts) >= curdate()
     and (
    callingpartyno in (select extension from ambition.ambition_users
    )
     or  finallycalledpartyno in (select extension from ambition.ambition_users
    )
    )
     group by
     id;


代替再次使用相同的子查询