给定一个select语句如何再选择更多?

carvr3hs  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(318)

下面的select语句提供了一个加班时间大于80的表:

SELECT empid,overtime FROM(SELECT empid, IF(SUM(slength)>80,SUM(slength)-80,0) 
as 'overtime' from schedule_2 group by empid) as t
where overtime >0;

[![输出][1]][1]
现在我想将empid与另一个表(t3)连接起来,在这个表中我必须连接名字和姓氏,然后将它连接到上面的表中。
[![此表][2]][2]
我似乎不知道如何连接这两个select语句并不断地出错

hgc7kmma

hgc7kmma1#

试试这个:

select empid, fullName, overtime
from (
    SELECT empid,
           overtime 
    FROM (
        SELECT empid, 
               IF(SUM(slength)>80,SUM(slength)-80,0) as overtime
        from schedule_2 group by empid
    ) as t where overtime > 0
) a join (
    select concat(firstName, ' ', lastName) fullName, 
           empid 
    from employees_2
) b on a.empid = b.empid
eagi6jfj

eagi6jfj2#

将您的查询用作子查询,并与表t3连接以供使用 concat 功能

select t1.empid,
concat(t3.firstname,t3.lastname) as name,
t1.overtime from    
(
select * from
(
SELECT empid, IF(SUM(slength)>80,SUM(slength)-80,0) 
as overtime from schedule_2 group by empid
) as t
where overtime >0
) as t1 join t3 on t1.empid=t3.empid
nx7onnlm

nx7onnlm3#

尝试使用下面的join with employees\ 2表

SELECT t.empid,overtime,concat(firstname,' ',lastname) as empname
FROM
(SELECT empid, IF(SUM(slength)>80,SUM(slength)-80,0) 
as 'overtime' from schedule_2 group by empid) as t
inner join employees_2 t1 on t.empid=t1.empid
where overtime >0;

相关问题