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 構文にエラーがあります。21 行目の「from PT_Tutors ts」付近で使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。

代わりに結合を使用する必要があるかどうかを確認するために同様の質問を投稿しましたが、サブクエリに戻ってきたので、誰かが私が間違っていることを教えてくれることを願っています.

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

CONCATの前に、閉じていない括弧を開きます。

select ts.id,(CONCAT(ts.first_name, ' ', ts.last_name),
于 2012-09-10T08:46:22.880 に答える
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 に答える