在 SQL 中删除姓名不超过 2 行的客户
Remove clients who don't have 2 rows by their name in SQL
我想做的是根据在数据库中注册两次的客户端进行过滤。这是因为我需要知道他们中谁至少来过两次,这就是为什么我使用 table 每次他们在系统中注册时都会注册的原因,如下所示:
order #
client
date
One
Andrew
XX
Two
Andrew
XX+1
Three
Andrew
XX+2
One
David
YY
One
Marc
ZZ
Two
Marc
ZZ+1
在这种情况下,我想删除 David 的记录,因为我只想要订单号不同于“one”的人。
我试过了 SQL:
select *
from table
where order_number > 1
然而,这样做是删除第一个订单的所有行,包括返回的那些。
有人知道一种简单的方法让我比较行名称并据此进行过滤,或者我怎样才能删除那些只有一个条目的客户的行?
你需要这样的东西:
select * from yourtable
where not exists (select 1 from yourtable where order_number >1)
或:
select client
from tablename
group by client
having count(*) > 1
CREATE TABLE records (
ID INTEGER PRIMARY KEY,
order_number TEXT NOT NULL,
client TEXT NOT NULL,
date DateTime NOT NULL
);
INSERT INTO records VALUES (1,'ONE', 'Adrew', '01.01.1999');
INSERT INTO records VALUES (2, 'TWO','Adrew', '02.02.1999');
INSERT INTO records VALUES (3, 'THREE','Adrew', '03.03.1999');
INSERT INTO records VALUES (4, 'ONE', 'David', '01.01.1999');
INSERT INTO records VALUES (5, 'ONE','Marc', '01.01.1999');
INSERT INTO records VALUES (6, 'TWO','Marc', '01.03.1999');
DELETE FROM records WHERE ID in
(
SELECT COUNT(client) as numberofclient FROM records
Group By client Having Count (client) = 1
);
我想做的是根据在数据库中注册两次的客户端进行过滤。这是因为我需要知道他们中谁至少来过两次,这就是为什么我使用 table 每次他们在系统中注册时都会注册的原因,如下所示:
order # | client | date |
---|---|---|
One | Andrew | XX |
Two | Andrew | XX+1 |
Three | Andrew | XX+2 |
One | David | YY |
One | Marc | ZZ |
Two | Marc | ZZ+1 |
在这种情况下,我想删除 David 的记录,因为我只想要订单号不同于“one”的人。
我试过了 SQL:
select *
from table
where order_number > 1
然而,这样做是删除第一个订单的所有行,包括返回的那些。
有人知道一种简单的方法让我比较行名称并据此进行过滤,或者我怎样才能删除那些只有一个条目的客户的行?
你需要这样的东西:
select * from yourtable
where not exists (select 1 from yourtable where order_number >1)
或:
select client
from tablename
group by client
having count(*) > 1
CREATE TABLE records (
ID INTEGER PRIMARY KEY,
order_number TEXT NOT NULL,
client TEXT NOT NULL,
date DateTime NOT NULL
);
INSERT INTO records VALUES (1,'ONE', 'Adrew', '01.01.1999');
INSERT INTO records VALUES (2, 'TWO','Adrew', '02.02.1999');
INSERT INTO records VALUES (3, 'THREE','Adrew', '03.03.1999');
INSERT INTO records VALUES (4, 'ONE', 'David', '01.01.1999');
INSERT INTO records VALUES (5, 'ONE','Marc', '01.01.1999');
INSERT INTO records VALUES (6, 'TWO','Marc', '01.03.1999');
DELETE FROM records WHERE ID in
(
SELECT COUNT(client) as numberofclient FROM records
Group By client Having Count (client) = 1
);