当前位置: 代码迷 >> Sql Server >> 为何在sql中会报错
  详细解决方案

为何在sql中会报错

热度:89   发布时间:2016-04-27 11:55:17.0
为什么在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的。

SQL code
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
------解决方案--------------------
SQL code
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)
------解决方案--------------------
其實二樓的改一點就可以了
SQL code
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
  相关解决方案