2012-09-10 82 views
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'附近使用正确的语法。

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

回答

1

你打开2个支架靠近你的concat函数,但只有接近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 
+0

谢谢!那么,我是否因为白痴而失去积分? –

+0

我很确定几乎每个人都发现自己在某些时候对这些错误感到头痛:) – Hazaart

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 
+0

John Woo - 如果我可能会问,通过使用Join可以获得什么?我真的不明白他们,虽然我试图找出他们。谢谢! –

0

你打开你不关闭括号中的CONCAT之前之前取出支架:

select ts.id,(CONCAT(ts.first_name, ' ', ts.last_name),