0

我有以下子查询,它们抛出错误

 select ts.id,(CONCAT(ts.first_name, ' ', ts.last_name),
 (    select SUM(hours*pay)

   from 
 PTAddedApp aa 
   where 
   aa.tutor_id = ts.id
   and year(aa.date) = year(now())
   and month(aa.date) = month(now())


 ),
 (select SUM(nt.hours*nt.rate)
   from PT_NT_Work_Hours nt 
   where 
   nt.tutor_id = ts.id
   and year(nt.date) = year(now())
   and month(nt.date) = month(now())
 ) 

 from PT_Tutors ts

我收到以下错误消息。我假设我在做一些愚蠢的事情 - 1064 - 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 21 行的“来自 PT_Tutors ts”附近使用正确的语法

我发布了一个类似的问题,看看我是否应该使用联接,他们将我指向子查询,所以我希望有人能让我知道我做错了什么。

4

3 回答 3

1

您在 concat 函数附近打开 2 个括号,但只关闭 1 个。删除“CONCAT”之前的括号

select ts.id,CONCAT(ts.first_name, ' ', ts.last_name),
 (    select SUM(hours*pay)

   from 
 PTAddedApp aa 
   where 
   aa.tutor_id = ts.id
   and year(aa.date) = year(now())
   and month(aa.date) = month(now())


 ),
 (select SUM(nt.hours*nt.rate)
   from PT_NT_Work_Hours nt 
   where 
   nt.tutor_id = ts.id
   and year(nt.date) = year(now())
   and month(nt.date) = month(now())
 ) 

from PT_Tutors ts
于 2012-09-10T08:44:21.740 回答
0

另一个解决方案是这样。

SELECT  ts.id,
        (CONCAT(ts.first_name, ' ', ts.last_name),
        aa.totalAddedApp.
        nt.totalNT
FROM    PT_Tutors ts
        LEFT JOIN
            (
                SELECT tutor_id, SUM(hours*pay) totalAddedApp
                FROM PTAddedApp
                WHERE  year(`date`) = year(now()) and 
                    month(`date`) = month(now())
                GROUP BY tutor_id
            ) aa ON aa.tutor_id = ts.id
        LEFT JOIN
            (
                select tutor_id, SUM(hours*rate) totalNT
                from PT_NT_Work_Hours
                where  year(`date`) = year(now()) and 
                    month(`date`) = month(now())
                GROUP BY tutor_id      
            ) nt ON nt. = ts.id
于 2012-09-10T08:41:12.457 回答
0

在 CONCAT 之前,您打开一个您没有关闭的括号:

select ts.id,(CONCAT(ts.first_name, ' ', ts.last_name),
于 2012-09-10T08:46:22.880 回答