日期:2014-05-18  浏览次数:20513 次

查询明细记录表相同父ID的记录ID,怎么简化?半天没写出来
SELECT           BatchDetail_ID
FROM                   BatchDetail
WHERE     BatchDetail_FaID   IN
(SELECT           BatchDetail_FaID
                                                        FROM                     BatchDetail
                                                        WHERE             BatchDetail_Operator   =   '17 ')
怎么自关联简化一下?

------解决方案--------------------
SELECT BatchDetail_ID
FROM BatchDetail as A,BatchDetail as B
WHERE A.BatchDetail_FaID=B.BatchDetail_FaID and A.BatchDetail_Operator = '17 '

这样行不?
------解决方案--------------------
SELECT BatchDetail_ID
FROM BatchDetail
WHERE exists (SELECT BatchDetail_FaID
FROM BatchDetail
WHERE BatchDetail_Operator = '17 ')

------解决方案--------------------
SELECT BatchDetail_ID
FROM BatchDetail b
WHERE EXISTS(SELECT * FROM BatchDetail
WHERE BatchDetail_FaID = b.BatchDetail_FaID AND
BatchDetail_Operator = '17 ')

------解决方案--------------------
select BatchDetail_ID FROM BatchDetail WHERE BatchDetail_Operator = '17 '和你的语句没啥区别啊。
------解决方案--------------------
SELECT DISTINCT A.BatchDetail_Id
FROM BatchDetail A INNER JOIN
(SELECT batchdetail_faId AS faid
FROM batchdetail
WHERE batchdetail_operator = '17 ') b ON A.BatchDetail_FaId = b.faid
------解决方案--------------------
SELECT BatchDetail_FaID,BatchDetail_ID
FROM BatchDetail
WHERE BatchDetail_Operator = '17 ' GROUP BY BatchDetail_FaID,BatchDetail_ID
------解决方案--------------------
是啊
select BatchDetail_ID FROM BatchDetail WHERE BatchDetail_Operator = '17 '
不和你上面的一样么?