select MID,m.HName,md.ToothKind,COUNT(md.ToothKind) as 数量,SUM(md.Price) as 总价 
from MDetail md
left outer join Mechanic m on m.ID=md.MID
where MID='1'
group by m.HName,md.ToothKind,MID
goselect count(*) from MDetail where MID='1'
以上两条SQL语句,为什么第一条使用联合查询得出的数量结果跟第二条语句的结果不一样呢?那要怎么取呢?==============================================================
第一条结果:
MID    HName  ToothKind  数量     总价 
-----------------------------------------
1      中医院 aaa  96  30912.00
1      中医院 ddd  164  .00
1      中医院 ccc  60  .00
==============================================================
第二条结果:
80
==============================================================

解决方案 »

  1.   

    count(distinct md.ToothKind) 试试
    你联查的话 如果右边有多条符合条件的 m.ID=md.MID的话 结果集中就会增加相应的行
      

  2.   

    那如果我要取Mechanic表在MDetail表中对应的ToothKind的数量,我那样写对吗?
      

  3.   

    select
      a.ToothKind,count(a.ToothKind) as 数量 
    from 
      Mechanic 
    a join MDetail b on
      a.ToothKind=b.ToothKind
      

  4.   

    ---和你的差不多
    select
      a.ToothKind,count(a.ToothKind) as 数量 
    from 
      Mechanic 
    a join MDetail b on
      a.ToothKind=b.ToothKind
    where 
      这里是你的一些附加条件 
    group by
      a.ToothKind
      

  5.   

    MDetail表的一个mid号,对应mechanic表的多个id号
    因为不是一一对应,统计出的结果应该不是你想要的结果。