选择列的子集

时间:2018-11-19 07:46:24

标签: sql sqlite

我正在使用sqllite创建两个表

CREATE TABLE NAMES(Id integer PRIMARY KEY, Name text);

CREATE TABLE PHONE(Id inetegr PRIMARY KEY,  PersonID integer, Number integer, 
FOREIGN KEY (PersonID) REFERENCES NAMES(Id)
 );

然后,我写了一篇题词,选拔所有拥有2部以上手机的人

select count(PersonID) as counts,Name 
from PHONE 
  INNER JOIN NAMES on NAMES.Id=PHONE.PersonID  
group by PersonID 
having counts>=2;

输出如下所示

2|Tom

但是,我不想打印计数而只需要名称,如何更改查询以仅打印名称。

2 个答案:

答案 0 :(得分:1)

您可以在下面尝试

DELETE FROM test WHERE ext_no IN 
(
SELECT   ext_no, MAX(value)
FROM     test
GROUP BY ext_no
HAVING   COUNT(*) > 2 AND 
         COUNT(*) = COUNT(CASE value WHEN 0 THEN 1 END)
);

答案 1 :(得分:1)

您可以尝试以下操作

select Name from PHONE 
INNER JOIN NAMES on NAMES.Id=PHONE.PersonID  
group by Name 
having count(distinct Number)>=2;