SQLite删除条件,Firefox历史记录

时间:2018-09-03 16:49:03

标签: sqlite firefox

如何删除此查询的结果?,复制这些条件,但使用DELETE。

此查询返回上次访问的网站,我将使用相同的条件删除历史记录,有必要将语法转换为“ DELETE FROM”并使用相同的条件,请帮助..谢谢。

SELECT datetime(moz_historyvisits.visit_date/1000000, 'unixepoch') AS 'Date Visited', moz_places.title AS Title, moz_places.url AS URL, moz_places.visit_count AS Count FROM moz_historyvisits, moz_places WHERE moz_historyvisits.place_id = moz_places.id ORDER BY moz_historyvisits.visit_date ASC

或这个

SELECT datetime(moz_historyvisits.visit_date/1000000,'unixepoch') as data, moz_places.url
FROM   moz_places, moz_historyvisits 
WHERE  moz_places.id = moz_historyvisits.place_id
ORDER BY 1 desc
LIMIT 21 OFFSET 0;

2 个答案:

答案 0 :(得分:0)

我不确定我是否已经完全理解您的问题,但是如果您想有选择地从moz_placesmoz_historyvisits删除关联的条目,那么您应该可以使用如下代码:

CREATE TEMPORARY VIEW delenda AS
  SELECT id FROM moz_places WHERE ($condition);

DELETE FROM moz_historyvisits WHERE place_id IN (SELECT id FROM delenda);
DELETE FROM moz_places WHERE id IN (SELECT id FROM delenda);

(用您要删除的条件替换$ condition。)

答案 1 :(得分:0)

您可以使用WITH ..... DELETE,其中.... CTE (公用表表达式),它等同于您的查询。或者,您可以将查询用作DELETE的WHERE子句的子查询

但是,查询所需的全部内容是删除,该数据足以识别要删除的行。假设表不是一个WITHOUT ROWID 表,那么所选表的 rowid 就足够了。

因此,以下方法将起作用:-

解决方案1(删除了...)

WITH cte1 AS (SELECT moz_historyvisits.rowid AS rid
FROM   moz_places, moz_historyvisits 
WHERE  moz_places.id = moz_historyvisits.place_id
ORDER BY 1 desc
LIMIT 21 OFFSET 0)
DELETE FROM moz_historyvisits WHERE moz_historyvisits.rowid IN (SELECT rid FROM cte1);
  • 请注意,datetime(moz_historyvisits.visit_date/1000000,'unixepoch') as data, moz_places.url是不需要的,因此已从CTE中排除(可以包括在内)。

解决方案2使用WHERE子句中的SubQuery进行删除

或者,您可以使用查询(同样只是检索rowid)作为WHERE子句的子查询,如下所示:-

DELETE FROM moz_historyvisits WHERE moz_historyvisits.rowid IN 
    (SELECT moz_historyvisits.rowid AS rid
    FROM   moz_places, moz_historyvisits 
    WHERE  moz_places.id = moz_historyvisits.place_id
    ORDER BY 1 desc
    LIMIT 21 OFFSET 0)
;

用于测试的示例代码

以下是用于测试上述内容的代码:-

DROP TABLE IF EXISTS moz_places;
DROP TABLE IF EXISTS moz_historyvisits;
CREATE TABLE IF NOT EXISTS moz_historyvisits (place_id INTEGER, visit_date);
CREATE TABLE IF NOT EXISTS moz_places (id INTEGER PRIMARY KEY, URL TEXT, title TEXT, visit_count INTEGER);


-- Add Some data
INSERT INTO moz_places (URL) VALUES('Rome'),('London'),('Paris'),('Sydney'),('Tokyo');
INSERT INTO moz_historyvisits VALUES(1,'2010-10-11 10:30'),(2,'2018-01-31 12:20'),(1,'2018-02-28 08:15'),(3,'2017-06-01 15:45');

-- Result 1 (show original data)
SELECT * FROM moz_historyvisits JOIN moz_places ON moz_historyvisits.place_id = moz_places.id;


-- Result 2 (test the CTE by selecting everything extracted by the CTE)
WITH cte1 AS (SELECT datetime(moz_historyvisits.visit_date/1000000,'unixepoch') AS data, moz_places.url, moz_places.id, moz_historyvisits.rowid AS rid
FROM   moz_places, moz_historyvisits 
WHERE  moz_places.id = moz_historyvisits.place_id
ORDER BY 1 desc
LIMIT 21 OFFSET 0)
SELECT * FROM cte1;

-- This does deleteion using WITH ..... DELETE
WITH cte1 AS (SELECT moz_historyvisits.rowid AS rid
FROM   moz_places, moz_historyvisits 
WHERE  moz_places.id = moz_historyvisits.place_id
ORDER BY 1 desc
LIMIT 21 OFFSET 0)
DELETE FROM moz_historyvisits WHERE moz_historyvisits.rowid IN (SELECT rid FROM cte1);

-- Result 43show table after deletion
SELECT * FROM moz_historyvisits JOIN moz_places ON moz_historyvisits.place_id = moz_places.id;

-- Do alternative/equivakent deletion via subquery
DELETE FROM moz_historyvisits WHERE moz_historyvisits.rowid IN(SELECT moz_historyvisits.rowid AS rid
FROM   moz_places, moz_historyvisits 
WHERE  moz_places.id = moz_historyvisits.place_id
ORDER BY 1 desc
LIMIT 21 OFFSET 0);

-- Result 4 show table after both deletions
SELECT * FROM moz_historyvisits JOIN moz_places ON moz_historyvisits.place_id = moz_places.id;

结果输出

结果1-

![enter image description here

结果2-

enter image description here

结果3-

enter image description here

结果4-

enter image description here

消息-

DROP TABLE IF EXISTS moz_places
> OK
> Time: 0.356s


DROP TABLE IF EXISTS moz_historyvisits
> OK
> Time: 0.324s


CREATE TABLE IF NOT EXISTS moz_historyvisits (place_id INTEGER, visit_date)
> OK
> Time: 0.358s


CREATE TABLE IF NOT EXISTS moz_places (id INTEGER PRIMARY KEY, URL TEXT, title TEXT, visit_count INTEGER)
> OK
> Time: 0.333s


-- Add Some data
INSERT INTO moz_places (URL) VALUES('Rome'),('London'),('Paris'),('Sydney'),('Tokyo')
> Affected rows: 5
> Time: 0.265s


INSERT INTO moz_historyvisits VALUES(1,'2010-10-11 10:30'),(2,'2018-01-31 12:20'),(1,'2018-02-28 08:15'),(3,'2017-06-01 15:45')
> Affected rows: 4
> Time: 0.354s


-- Result 1 (show original data)
SELECT * FROM moz_historyvisits JOIN moz_places ON moz_historyvisits.place_id = moz_places.id
> OK
> Time: 0s


-- Result 2 (test the CTE by selecting everything extracted by the CTE)
WITH cte1 AS (SELECT datetime(moz_historyvisits.visit_date/1000000,'unixepoch') AS data, moz_places.url, moz_places.id, moz_historyvisits.rowid AS rid
FROM   moz_places, moz_historyvisits 
WHERE  moz_places.id = moz_historyvisits.place_id
ORDER BY 1 desc
LIMIT 21 OFFSET 0)
SELECT * FROM cte1
> OK
> Time: 0s


-- Do alternative/equivakent deletion via subquery
DELETE FROM moz_historyvisits WHERE moz_historyvisits.rowid IN(SELECT moz_historyvisits.rowid AS rid
FROM   moz_places, moz_historyvisits 
WHERE  moz_places.id = moz_historyvisits.place_id
ORDER BY 1 desc
LIMIT 21 OFFSET 0)
> Affected rows: 4
> Time: 0.228s


-- Result 4 show table after both deletions
SELECT * FROM moz_historyvisits JOIN moz_places ON moz_historyvisits.place_id = moz_places.id
> OK
> Time: 0s


-- This does deleteion using WITH ..... DELETE
WITH cte1 AS (SELECT moz_historyvisits.rowid AS rid
FROM   moz_places, moz_historyvisits 
WHERE  moz_places.id = moz_historyvisits.place_id
ORDER BY 1 desc
LIMIT 21 OFFSET 0)
DELETE FROM moz_historyvisits WHERE moz_historyvisits.rowid IN (SELECT rid FROM cte1)
> Affected rows: 0
> Time: 0s


-- Result 43show table after deletion
SELECT * FROM moz_historyvisits JOIN moz_places ON moz_historyvisits.place_id = moz_places.id
> OK
> Time: 0s