且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找符合特定条件的记录组

更新时间:2022-06-22 10:22:01

SELECT
  ID,
  ParentID,
  DataValue
FROM
  MyTable
WHERE
  NOT EXISTS (
    SELECT 1 
      FROM MyTable i
     WHERE i.ParentId = MyTable.ParentId AND i.DataValue = 'A'
  )

如果表很大,建议使用 (ParentId, DataValue) 上的索引.

An index over (ParentId, DataValue) is recommendable if the table is large.