选择查询联接或分组依据

时间:2019-05-03 18:03:47

标签: oracle

我有3张桌子,我想做一个选择语句,并显示患者的名字,账单状态和方法,他如何付款以及他付款的金额,但是我很难尝试进行查询。

```` Create table patient (
```` Patient_ID Number(9) primary key,
```` First_name varchar2(15),
```` Last_name varchar2(10),
```` Contact number(10),
```` City varchar2(20),
```` Doctor_ID Number(9) references Doctor(Doctor_ID));

This is the payment table 
````Payment_ID Number(9) primary key,
````Amount Number(10),
````Pyt_Date Date,
````Method varchar2(15),
````PatientID Number(9) references Patient(Patient_ID));

This is the bill table
````Bill_ID Number(9) primary key,
````Status char(10),
````Amount_paid varchar(30),
````Amount_NotPaid varchar(30),
````PatientID Number(9) references Patient(Patient_ID));

This is the query I wrote but it's not working

```` select p.First_name "Patient", Status, method, Amount_paid
```` from patient p and payment on bill 
```` where p.Firstname = status, method = amount_paid
```` group by p.First_name, status, method, amount_paid;

1 个答案:

答案 0 :(得分:0)

您编写的查询在语法上无效。您写道:

select p.First_name "Patient", Status, method, Amount_paid
  from patient p and payment on bill 
  where p.Firstname = status, method = amount_paid
  group by p.First_name, status, method, amount_paid

在不允许的FROM子句中有一个AND,而在需要的WHERE子句中缺少AND:

select p.First_name "Patient", b.Status, b.method, b.Amount_paid
  from patient p,
       bill b
  where p.Firstname = b.status AND
        b.method = b.amount_paid
  group by p.First_name, b.status, b.method, b.amount_paid

但是我真的怀疑这会按照您的意愿去做,因为我非常怀疑计费方式是否与所支付的金额相匹配,或者患者的名字是否与计费状态相匹配。

也许你的意思是

SELECT p.FIRST_NAME, b.STATUS, y.METHOD, b.AMOUNT_PAID
  FROM PATIENT p
  INNER JOIN BILL b
    ON b.PATIENTID = p.PATIENT_ID
  INNER JOIN PAYMENT y
    ON y.PATIENTID = p.PATIENT_ID

如果您要编辑问题以包括每个表的示例数据以及示例数据的预期结果,这将有所帮助。谢谢。