2017-08-08 84 views
1

刪除多行我有一個子查詢,因爲這:怎樣從一個表中的SQLite

SELECT INTER1.LID1 AS ILID1,INTER1.LID2 AS ILID2 FROM 
(SELECT L1.ID1 AS LID1, L1.ID2 AS LID2 FROM Friend F1 
INNER JOIN Likes L1 
ON F1.ID1 = L1.ID1 AND F1.ID2 = L1.ID2) INTER1 
LEFT JOIN Likes L2 
ON (INTER1.LID1 = L2.ID2 AND INTER1.LID2 = L2.ID1) 
WHERE (L2.ID1 IS NULL AND L2.ID2 IS NULL) 

這樣做的輸出如下:

 ID1 ID2 

    1641 1468 
    1911 1247 

現在我要刪除這兩個從表中這些行LIKES其格式如下:

ID1  ID2 

1689  1709 
1709  1689 
1782  1709 
1911  1247 
1247  1468 
1641  1468 

我試圖此使用以下查詢SQLite中來完成:

DELETE FROM Likes 
WHERE EXISTS 
(SELECT INTER1.LID1 AS ILID1,INTER1.LID2 AS ILID2 FROM 
(SELECT L1.ID1 AS LID1, L1.ID2 AS LID2 FROM Friend F1 
INNER JOIN Likes L1 
ON F1.ID1 = L1.ID1 AND F1.ID2 = L1.ID2) INTER1 
LEFT JOIN Likes L2 
ON (INTER1.LID1 = L2.ID2 AND INTER1.LID2 = L2.ID1) 
WHERE (L2.ID1 IS NULL AND L2.ID2 IS NULL)); 

但它不按預期工作,它不是隻刪除那些行。

然後我試着用INNER JOIN來解決這個問題,但是在DELETE函數中INNER JOIN的功能在SQLite中沒有。

我該如何做這項工作。

請幫助

回答

1

就在一次兩個值進行比較:

DELETE FROM Likes 
WHERE (ID1, ID2) IN (SELECT ... your original query ...); 

Row values需要SQLite的3.15或更高版本。 如果您使用的是較早的版本,你要查找刪除一個單列主鍵(rowid,或者你已經聲明爲INTEGER PRIMARY KEY)在一個單獨的步驟行:

DELETE FROM Likes 
WHERE rowid IN (SELECT Likes.rowid 
       FROM Likes 
       JOIN (SELECT ... your original query ...) AS IDs 
        ON Likes.ID1 = IDs.ILID1 
       AND Likes.ID2 = IDs.ILID2); 
+0

我在線學習斯坦福大學的Lagunita SQL課程,似乎他們在指定之前使用SQLite版本來完成這項工作。是否有替代方案? –