2015-06-10 177 views
-1

我創建了兩個具有以下記錄的表。Sqllite查詢重複記錄

CREATE TABLE ForgeRock 
    (`id` int,`sta` int, `productName` varchar(7), `description` varchar(55)); 

INSERT INTO ForgeRock 
    (`id`,`sta`, `productName`, `description`) 
VALUES 
    (1, 0,'OpenIDM', 'Platform for building enterprise provisioning solutions'), 

(2,0, 'OpenAM', 'Full-featured access management'), 
(3,0, 'OpenDJ', 'Robust LDAP server for Java'); 


CREATE TABLE ForgeRock1 
(`id` int,`sta` int, `productName` varchar(7), `description` varchar(55)); 

INSERT INTO ForgeRock1 
(`id`,`sta`, `productName`, `description`) 

價值觀

(1, 2,'hii', 'Platform for building enterprise provisioning solutions'), 
(2,0, 'OpenAM', 'Full-featured access management'), 
(3,0, 'OpenDJ', 'Robust LDAP server for Java'); 

我想聯盟的兩個表,但希望有STA重複ID記錄= 2

SELECT id,sta 
    FROM ForgeRock 
    GROUP BY id 
    UNION 
    SELECT id, sta 
    FROM ForgeRock1 
    GROUP BY id 

結果:

id sta 1 0 2 0 3 0 1 2

預期結果:

id sta 1 2 2 0 3 0

回答

0

執行分組的UNION,並使用MAX()來選擇行,以輸出每個組:

SELECT id, MAX(sta) AS sta 
FROM (SELECT id, sta 
     FROM ForgeRock 
     UNION 
     SELECT id, sta 
     FROM ForgeRock1) 
GROUP BY id 
ORDER BY id