如何删除此查询的结果?,复制这些条件,但使用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;
我不确定我是否完全理解你的问题,但是如果你想有选择地删除moz_places
和moz_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替换为您的删除标准。)
您可以使用WITH ..... DELETE
,其中....
是CTE(公用表表达式),它等同于您的查询。或者,您可以将查询用作DELETE的WHERE子句的子查询。
但是,您只需要从查询中删除足够的数据来标识要删除的行。假设该表不是WITHOUT ROWID表,那么所选表的rowid就足够了。
因此,以下方法可行: -
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之外(可以包含它们)。或者,您可以使用查询(再次只是检索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 -
结果2 -
结果3 -
结果4 -
消息 -
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