Sqlite 根据计数从表中选择

Sqlite 根据计数从表中选择,sqlite,select,count,Sqlite,Select,Count,我有这样的桌子: id | name | type ----------------- 0 | firs | 2 1 | secs | 3 2 | this | 9 1 | thus | 3 我知道id(它不是唯一的id)和类型,仅当有指定数量的记录具有该id和类型时,我才想选择记录。 例如,我尝试了一条记录: select * from myTable where (select count(*) from myTable where myTable.id =

我有这样的桌子:

id | name | type
-----------------
 0 | firs |    2
 1 | secs |    3
 2 | this |    9
 1 | thus |    3
我知道id(它不是唯一的id)和类型,仅当有指定数量的记录具有该id和类型时,我才想选择记录。
例如,我尝试了一条记录:

select * from myTable
where
(select count(*) from myTable where myTable.id = 0 and myTable.type = 2) = 1;

这将返回所有行,而不仅仅是我想要的一行。有人能告诉我,什么是获得正确结果的正确方法吗?

您的where语句看起来像where 1=1,它总是正确的,因此SELECT会从表中获取每一列。 如果要选择id=1、类型=3、出现次数=2的示例数据,可以执行以下操作:

select * from myTable
where
(select count(*) from myTable where myTable.id = 1 and myTable.type = 3) = 2 and myTable.id = 1 and myTable.type = 3;

你是对的。我忘了它是内部选择,所以我必须将列条件也添加到外部选择。谢谢