首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

为何在sql中会报错

2012-08-15 
为什么在sql中会报错下面语句是找出cardtran中重复的记录,我在oracle中可以运行,但在sql2k中一直报错,不知

为什么在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 

热点排行