SQL错误:ORA-00936:缺少表达式,缺少表达式在哪里?

时间:2013-10-21 09:15:47

标签: sql oracle

Qns:项目描述和任何名为Jessie Stange的患者的治疗日期(即GivenName是Jessie& FamilyName是Stange)

我写的:

SELECT DISTINCT 
  Description, 
  Date as treatmentDate 
WHERE doothey.Patient P, 
      doothey.Account A, 
      doothey.AccountLine AL, 
      doothey.Item.I AND 
      P.PatientID = A.PatientID AND 
      A.AccountNo = AL.AccountNo AND 
      AL.ItemNo = I.ItemNo AND 
     (p.FamilyName = 'Stange' AND p.GivenName = 'Jessie');

错误:

  

命令行错误:1列:30错误报告:SQL错误:ORA-00936:缺少表达式00936. 00000 - “缺少表达式”*原因:*操作:

缺少什么表达?

2 个答案:

答案 0 :(得分:4)

正确的语法如下。

SELECT DISTINCT Description, Date as treatmentDate 
FROM
 doothey.Patient P, doothey.Account A, doothey.AccountLine AL, doothey.Item I 
WHERE P.PatientID = A.PatientID AND A.AccountNo = AL.AccountNo 
AND AL.ItemNo = I.ItemNo    
AND (p.FamilyName = 'Stange' AND p.GivenName = 'Jessie');

虽然上面的查询可行,但推荐的方法是使用ANSI SQL JOIN语法

SELECT DISTINCT Description, Date as treatmentDate 
FROM doothey.Patient P
INNER JOIN doothey.Account A
    ON P.PatientID = A.PatientID 
INNER JOIN doothey.AccountLine AL
    ON A.AccountNo = AL.AccountNo
INNER JOIN doothey.Item I 
    ON AL.ItemNo = I.ItemNo 
WHERE   
 p.FamilyName = 'Stange' AND p.GivenName = 'Jessie';

答案 1 :(得分:0)

正如人们所评论的那样,您错过了选择中的FROM声明。

我还建议只是在FROM中声明所有表格并在WHERE子句中指定链接,而是使用join表格一起使用LEFT JOIN和/或INNER JOIN等等。

以下是我认为应该涵盖我上面试图解释的问题。

SELECT DISTINCT 
    Description, 
    Date as treatmentDate 
FROM
    doothey.Patient P
INNER JOIN
    doothey.Account A
ON
    P.PatientID = A.PatientID    
INNER JOIN
    doothey.AccountLine AL
ON
    A.AccountNo = AL.AccountNo
INNER JOIN
    doothey.Item.I  
ON
    AL.ItemNo = I.ItemNo
WHERE 
    p.FamilyName = 'Stange' 
AND 
    p.GivenName = 'Jessie'