0

我有两(2)张桌子。第一个命名TABLE有 N 行,唯一的 _A (KEY),第二个命名TABLE_AUX有 0 行,然后我只是运行下一个查询:

INSERT INTO TABLE_AUX(_A,_B,_C) 
SELECT A,B,C FROM TABLE;

评估查询后,我有:

a) N = SELECT COUNT(1) FROM TABLE = SELECT COUNT(1) FROM TABLE_AUX

b) K = SELECT SUM(Q.CNT-1) 
       FROM ( SELECT COUNT(1) as CNT, _A 
              FROM TABLE_AUX GROUP BY _A HAVING COUNT(1)>1
       ) Q;

c) M = SELECT COUNT(Q._A) 
       FROM (SELECT DISTINCT _A 
             FROM TABLE_AUX) Q

, WHERE N = M + K, then M < N 

d) Zero = 0 = SELECT COUNT(1) 
              FROM TABLE T 
              WHERE NOT EXISTS ( SELECT * 
                                 FROM TABLE_AUX TA 
                                 WHERE TA._A = T._A );

e) P = SELECT COUNT(1) FROM TABLE T, TABLE_AUX TA WHERE T._A = TA._A;

, WHERE P = N + K, then N < P

很奇怪,在某些情况下会出现重复的结果。但并非在所有情况下。有人知道会发生什么吗?

4

1 回答 1

0

在 Select 查询中使用 Distinct 关键字。

INSERT INTO TABLE_AUX(_A,_B,_C) 
SELECT DISTINCT A,B,C FROM TABLE;
于 2013-10-26T12:34:56.957 回答