0

次の mysql ステートメントを検討してください。

SELECT costs.cost AS package, payments.cost AS labs, patients.fname, patients.lname, patients.sname, visit.id AS visitid, patients.id, pharm_payments.cost AS pharm_costs ,costs.cost+payments.cost+pharm_payments.cost AS total_cost ,
CASE pharm_payments.cost WHEN pharm_payments.visitid=visit.id THEN pharm_payments.cost
WHEN pharm_payments.visitid != visit.id THEN 0
CASE costs.cost WHEN cost.visitid=visit.id THEN costs.cost
WHEN costs.visitid != visit.id THEN 0
FROM costs
LEFT JOIN visit ON costs.visitid = visit.id
LEFT JOIN patients ON visit.patientid = patients.id
LEFT JOIN pharm_payments ON pharm_payments.visitid = visit.id
LEFT JOIN payments ON payments.visitid = visit.id
WHERE costs.paid =  'not paid'
AND visit.VisitDate >= CURDATE( ) 
LIMIT 0 , 30

クエリから期待される結果は、訪問 ID が同じでない場所である必要があり、ゼロの値を返す必要があります。次のエラーが発生し続けます:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE costs.cost WHEN cost.visitid=visit.id THEN costs.cost WHEN costs.visitid !=' at line 4

このエラーを解決するにはどうすればよいですか?

4

3 に答える 3

0
SELECT 
  costs.cost AS package,
  payments.cost AS labs,
  patients.fname,
  patients.lname,
  patients.sname,
  visit.id AS visitid,
  patients.id,
  pharm_payments.cost AS pharm_costs,
  costs.cost + payments.cost + pharm_payments.cost AS total_cost,
  CASE
    pharm_payments.cost 
    WHEN pharm_payments.visitid = visit.id 
    THEN pharm_payments.cost 
    WHEN pharm_payments.visitid != visit.id 
    THEN 0 END AS  pharm_payments_cost,
    CASE
      costs.cost 
      WHEN cost.visitid = visit.id 
      THEN costs.cost 
      WHEN costs.visitid != visit.id 
      THEN 0 END AS costs_cost 
FROM
  costs 
  LEFT JOIN visit 
    ON costs.visitid = visit.id 
  LEFT JOIN patients 
    ON visit.patientid = patients.id 
  LEFT JOIN pharm_payments 
    ON pharm_payments.visitid = visit.id 
  LEFT JOIN payments 
    ON payments.visitid = visit.id 
WHERE costs.paid = 'not paid' 
  AND visit.VisitDate >= CURDATE() 
LIMIT 0, 30 
于 2013-10-28T10:49:59.883 に答える
0

caseSQL ステートメントでの使用に慣れていませんが、 afterを付けるべきではありませんか?END

于 2013-10-28T10:27:33.097 に答える
0

ケースステートメントは次のようにする必要があると思います:

.......
CASE 
    WHEN pharm_payments.visitid=visit.id 
    THEN pharm_payments.cost
    ELSE 0
END AS Column1,
CASE 
    WHEN cost.visitid=visit.id 
    THEN costs.cost
    ELSE 0
END AS Column2

アップデート

それらを合計するには、これを行うことができます:

.......
(
   CASE 
       WHEN pharm_payments.visitid=visit.id 
       THEN pharm_payments.cost
       ELSE 0
   END
) 
+
(
   CASE 
       WHEN cost.visitid=visit.id 
       THEN costs.cost
       ELSE 0
   END 
)AS total
于 2013-10-28T10:27:37.160 に答える