我有两张桌子医生和账单。我希望查看治疗一名以上病人的医生的dname,doctorid和总和(费用)。
医生表栏如下:doctorid,dname
结算表格列如下:
billid,patientid,doctorid,fees
我尝试了以下代码。但得到一个错误,指出SQL cmd没有正确结束:
SELECT d.dname,b.doctorid,Sum(b.fees)as "TotalFees" FROM billing b,doctor d ON b.doctorid=d.doctorid
GROUP BY b.doctorid HAVING count(b.patientid)>1;
我的语法出错了? 感谢...
答案 0 :(得分:1)
FROM billing b,doctor d ON b.doctorid=d.doctorid
应该是
FROM billing b inner join doctor d ON b.doctorid=d.doctorid
答案 1 :(得分:1)
您错过了INNER JOIN
关键字,还需要GROUP BY
SELECT
子句中GROUP BY d.dname, b.doctorid
子句中使用的所有非汇总列Sum(b.fees)
这种情况)。
此外,它可能是拼写错误,但您在as
和SELECT d.dname, b.doctorid, Sum(b.fees) as "TotalFees"
FROM billing b INNER JOIN doctor d ON b.doctorid=d.doctorid
GROUP BY d.dname, b.doctorid HAVING count(b.patientid)>1;
之间没有空格:
FROM
请记住,在WHERE
子句中使用逗号分隔的表名将执行笛卡尔积,这将是性能杀手,具体取决于您的RDBM优化器。如果您想使用它,请记住将其连接条件放在ON
子句中(JOIN
子句与SELECT d.dname, b.doctorid, Sum(b.fees) as "TotalFees"
FROM billing b, doctor d
WHERE b.doctorid = d.doctorid
GROUP BY d.dname, b.doctorid HAVING count(b.patientid)>1;
语法一起使用):
{{1}}
答案 2 :(得分:1)
您必须在查询中提及您要使用的联接,您必须更改您的查询,如
SELECT d.dname, b.doctorid, Sum(b.fees) as "TotalFees"
FROM billing b inner join doctor d ON b.doctorid=d.doctorid
GROUP BY d.dname, b.doctorid HAVING count(b.patientid)>1;
检查Join
答案 3 :(得分:1)
试试这个
SELECT d.dname,b.doctorid,Sum(b.fees)as "TotalFees" FROM billing b inner join doctor d ON b.doctorid=d.doctorid
GROUP BY d.dname,b.doctorid HAVING count(b.patientid)>1;