2017-01-03 70 views
0

從這個數據庫stucture ...鮮明WHERE語句得到重挫

CREATE TABLE points (
    rowid INTEGER PRIMARY KEY, 
    powId INTEGER, 
    userId INTEGER, 
    timeStart DATETIME, 
    timeEnd DATETIME, 
    points NUMERIC, 
    COUNTYNUM NUMERIC, 
-- FOREIGN KEY(powId) REFERENCES points_power(powId) 
-- FOREIGN KEY(userId) REFERENCES users(userId) 
    UNIQUE(userId, timeStart, COUNTYNUM) ON CONFLICT IGNORE 
); 

有了這些數據...

INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('1','7','2','2016-12-31T19:29:23-0500','2016-12-31T20:29:23-0500','2','201612311929'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('2','7','4','2016-12-31T19:29:23-0500','2016-12-31T20:29:23-0500','2','201612311929'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('3','7','12','2016-12-31T19:29:23-0500','2016-12-31T20:29:23-0500','2','201612311929'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('4','7','16','2016-12-31T19:29:23-0500','2016-12-31T20:29:23-0500','2','201612311929'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('5','7','585','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('6','7','2','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('7','7','12','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('9','7','11','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('10','7','593','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('11','7','14','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('12','7','13','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('13','7','11','2017-01-02T13:50:48-0500','2017-01-02T14:50:48-0500','2','201701021350'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('14','7','16','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('15','7','16','2017-01-01T00:33:46-0500','2017-01-01T01:33:46-0500','2','201701010033'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('16','7','2','2017-01-02T13:50:48-0500','2017-01-02T14:50:48-0500','2','201701021350'); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('17','13','1','2017-01-02T17:00:00-0500','2017-01-02T21:00:00-0500','1',NULL); 
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('18','13','2','2017-01-02T17:00:00-0500','2017-01-02T21:00:00-0500','1',NULL); 

並使用該查詢

SELECT DISTINCT powId, timeStart, timeEnd FROM points WHERE userId != 1;

我米沒有得到我想要的結果。

數據庫中的最後兩項我現在正在查看的內容。 userId爲這兩個條目中的第一個設置,但第二個條目掩蓋了userId,並且它現在仍然返回該不同的行。有什麼我可以做的查詢,使它不會返回任何不同的行,其中userId是在DISTINCT集?

我基本上想要得到沒有分配userId的所有點。我怎麼做?我認爲這會很簡單,但事實並非如此。

請只有SQLite的答案。

回答

1

你必須檢查與相同的列值的所有(其他)行:

SELECT DISTINCT powId, 
       timeStart, 
       timeEnd 
FROM points 
WHERE NOT EXISTS (SELECT 1 
        FROM points AS p2 
        WHERE p2.powId  = points.powId 
        AND p2.timeStart = points.timeStart 
        AND p2.timeEnd = points.timeEnd 
        AND p2.userId = 1);