沧海笑的艺术人生

沧海一声笑, 滔滔两岸潮,浮沉随浪只记今朝
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

几种t_sql语句写法

Posted on 2005-05-25 20:54  沧海笑  阅读(560)  评论(0编辑  收藏  举报
1。查询一个两列表中一列相同另一列不同的记录:
      设两列分别为a,b,表tbl则:
      SELECT i_PO,s_idnumber,i_DepartmentID 
      FROM ksb_receive  
      WHERE s_idnumber IN  
      (SELECT s_idnumber  FROM  (SELECT s_idnumber,COUNT(i_departmentid) AS count1  FROM  
      (SELECT s_idnumber,i_departmentid
      FROM ksb_receive GROUP BY s_idnumber,i_departmentID   HAVING len(s_idnumber)>0) AS a
     WHERE i_departmentid<>''  GROUP BY s_idnumber  HAVING COUNT(i_departmentid)>1  )AS m )
     GO

2。查询物料主表和收料表物料类别不同的记录(物料主表,收料表,关联表)
      设主表为a,物料表b,关联表c。则:
      SELECT m.IDNumber,m.FinMaterialType,n.s_FIMatType
      FROM tblmmd m  INNER JOIN
      (SELECT s_idnumber,i_departmentid,s_FIMatType
      FROM ksb_receive a   INNER JOIN FI_FIMatType b   ON a.i_departmentid = b.i_sort2
         AND a.i_departmentid IN (1,2,3)) n   
      ON m.idnumber = n.s_idnumber AND  m.FinMaterialType <> n.s_FIMatType