为什么在sql中会报错
下面语句是找出cardtran中重复的记录,我在oracle中可以运行,但在sql2k中一直报错,不知道错在什么地方,请各位大虾指教了。
SELECT *
FROM cardtran
WHERE (cardno, balance) IN
(SELECT cardno, balance
FROM cardtran
GROUP BY (cardno, balance)
HAVING COUNT(*) > 1
[解决办法]
SELECT *
FROM cardtran
WHERE (cardno, balance) IN
(SELECT cardno, balance
FROM cardtran
GROUP BY (cardno, balance)
HAVING COUNT(*) > 1)
不能这样IN的。
SELECT a.*FROM cardtran a join (SELECT cardno, balance FROM cardtran GROUP BY (cardno, balance) HAVING COUNT(*) > 1) b on a.cardno = b.cardno and a.balance = b.balance
[解决办法]
select t1.*from cardtran t1where exists (select 1 from cardtran t2 where t1.cardno=t2.cardno and t1.balance=t2.balance group by t2.cardno,t2.balance having count(1)>1)
[解决办法]
其實二樓的改一點就可以了
SELECT a.*FROM cardtran a join (SELECT cardno, balance FROM cardtran GROUP BY cardno, balance--去掉括號 HAVING COUNT(*) > 1) b on a.cardno = b.cardno and a.balance = b.balance