我一直在尝试教自己一些基本的SQL,以实现我正在使用数据库管理创建的应用程序中。
实施后很快,我意识到我不确定如何处理重复的条目。从网上看,似乎我需要为每个可能重复的数据分配一个“唯一”约束,但是我想知道是否可以用不同的方法来处理它。
最后,我想出了一个解决此问题的不同想法,我不确定如何用SQL编写它,即使这是一个可行的想法。>
我希望获得一些外部意见,以帮助我完成任务或反驳我的想法。
使用dart语言,我可以查找所有重复的条目,以及查找多少 存在重复项。这就是我用来获得此结果的方法:
SELECT `title`, COUNT(*) FROM `Article` GROUP BY `url` HAVING COUNT(*) > 1
这就是结果
flutter: [{title: US Stock Market Chalks up Huge Weekly Losses; Bitcoin, DXY Impress, COUNT(*): 2}, {title: What Triggered Ethereum to Rise 66% Within 1 Week? Factors and Trends, COUNT(*): 26}, {title: What’s Next for Crypto Miners?, COUNT(*): 2}]
我也许可以删除所有重复的结果,但返回的数量为
COUNT(*)
减一?这意味着如果我有26个重复的
What Triggered Ethereum to Rise 66% Within 1 Week? Factors and Trends
,我可以 删除COUNT(*)
代表的数字,即26,但之前 从数字26中删除负1,实际上使我剩下一个 剩下的重复吗? (这是使用 前面提到的“唯一”约束)
编辑:这是SQL行,用于向数据库中输入新的一行数据。
'INSERT INTO Article(title, description, url, urltoimage, publishedat) VALUES("$title", "$description", "$url", "$urlToImage", "$publishedAt")'
答案 0 :(得分:1)
您可以删除每个网址的所有bar 1条目 -假设url列应该是唯一的 -如果不仅是url列或另一列的组合,只需更改前两个WHERE子句即可。 -还假定该表不是WITHOUT ROWID表。
:-
SELECT count() FROM article WHERE url = 'myurl';
WITH cte AS (
SELECT rowid AS thisrowid
FROM article
WHERE url = 'myurl'
ORDER by url
LIMIT (SELECT count(*)-1 FROM article WHERE url = 'myurl')
)
DELETE FROM article WHERE rowid IN (SELECT thisrowid FROM cte);
这将使用CTE(公用表表达式(临时表))从<下的所有小节1行提取 rowid (除非指定了WITHOUT ROWID,否则始终存在的特殊的通常隐藏的特殊列)。根据选择条件(WHERE子句)(在这种情况下,其中url列包含myurl)来确定strong> article 表。即使用LIMIT,这是根据相同WHERE子句的行数减去1。
此CTE(rowid的列表)随后用作DELETE的WHERE子句的基础。
以上内容已使用以下SQL测试:-
DROP TABLE IF EXISTS article;
CREATE TABLE IF NOT EXISTS article (title TEXT, description TEXT, url TEXT, urltoimage TEXT, publisheddat);
INSERT INTO article VALUES
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01')
;
SELECT count() FROM article WHERE url = 'myurl';
WITH cte AS (
SELECT rowid
FROM article
WHERE url = 'myurl'
ORDER by url
LIMIT (SELECT count(*)-1 FROM article WHERE url = 'myurl')
)
-- SELECT * FROM cte;
DELETE FROM article WHERE rowid IN (SELECT * FROM cte);
SELECT count() FROM article WHERE url = 'myurl';
删除前的行数:-
剩余行数:-
说可能很容易
代码可以是(再次假设url列需要唯一):-
DROP TABLE IF EXISTS article_new;
DROP TABLE IF EXISTS article_old;
CREATE TABLE IF NOT EXISTS article_new (title TEXT, description TEXT, url TEXT UNIQUE, urltoimage TEXT, publisheddat);
INSERT OR IGNORE INTO article_new SELECT * FROM article;
ALTER TABLE article RENAME TO article_old;
ALTER TABLE article_new RENAME TO article;
DROP TABLE IF EXISTS article_old;
DROP TABLE IF EXISTS article;
CREATE TABLE IF NOT EXISTS article (title TEXT, description TEXT, url TEXT, urltoimage TEXT, publisheddat);
INSERT INTO article VALUES
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl','myimageurl','2018-01-01'),
('Title001','Description001','myurl2','myimageurl','2018-01-01'),
('Title001','Description001','myurl3','myimageurl','2018-01-01'),
('Title001','Description001','myurl3','myimageurl','2018-01-01'),
('Title001','Description001','myurl3','myimageurl','2018-01-01'),
('Title001','Description001','myurl3','myimageurl','2018-01-01'),
('Title001','Description001','myurl3','myimageurl','2018-01-01')
;
DROP TABLE IF EXISTS article_new;
DROP TABLE IF EXISTS article_old;
CREATE TABLE IF NOT EXISTS article_new (title TEXT, description TEXT, url TEXT UNIQUE, urltoimage TEXT, publisheddat);
INSERT OR IGNORE INTO article_new SELECT * FROM article;
ALTER TABLE article RENAME TO article_old;
ALTER TABLE article_new RENAME TO article;
DROP TABLE IF EXISTS article_old;
SELECT * FROM article;