mysql总和与第三个表不正确

时间:2014-02-12 15:55:50

标签: mysql

我有三个表都有一个唯一的id,就是pat_id。如果我使用这个stattement:

SELECT 
  bill_item,
  a.pat_id,
  SUM(bill_amount) 
FROM
  emr_bill a,
  emr_patient b 
WHERE a.pat_id = b.pat_id 
  AND bill_item LIKE '%consultation%' 
  AND DATE(bill_date) = '2013-11-01' 
GROUP BY b.pat_id,
  bill_item 

结果是我正在寻找的

| bill_item                   |pat_id |sum(bill_amount)  |

|Consultation Fee             |343    |         10.00    |

|Consultation Fee             |579    |         20.00    |

|Consultation Fee             |631    |         10.00    |

|Specialist consultations     |631    |        300.00    |

但是当我尝试从另一个表添加一个列时,如:

SELECT 
  bill_item,
  a.pat_id,
  SUM(bill_amount),
  vit_note 
FROM
  emr_bill a,
  emr_patient b,
  emr_vitals c 
WHERE a.pat_id = b.pat_id 
  AND a.pat_id = c.pat_id 
  AND b.pat_id = c.pat_id 
  AND c.cur_date = '2013-11-01' 
  AND bill_item LIKE '%consultation%' 
  AND DATE(bill_date) = '2013-11-01' 
GROUP BY b.pat_id,
  bill_item 

总和值列更改:

| bill_item                   |pat_id |sum(bill_amount)  | vit_note |

|Consultation Fee             |343    |         10.00    | By Me    |

|Consultation Fee             |579    |         20.00    | By Dan   |

|Consultation Fee             |631    |         70.00    | By Mike  |

|Specialist consultations     |631    |        700.00    | By Fred  |

当我从另一个表中添加第三列时,是否可以帮助我获取总和不变。

谢谢

0 个答案:

没有答案