基于数字过滤结果集

Posted

技术标签:

【中文标题】基于数字过滤结果集【英文标题】:Filtering result set based on a number 【发布时间】:2019-01-23 06:59:15 【问题描述】:

鉴于此结果集,如何根据唯一的 mmatter 列仅提取那些数字大于 2 但在 mdline 列中没有 1 的条目的条目?

这里的 SQL 小提琴:http://sqlfiddle.com/#!18/8c17e/2

查询:

SELECT top 1000
       [mdindex]
      ,[mmatter]
      ,[mdline]
      ,[mddesc]
FROM [desc]
WHERE [mmatter] IN (
    SELECT [mmatter]
    FROM [desc]
    GROUP BY [mmatter]
     HAVING COUNT(distinct [mdline]) > 1
)
order by mmatter

结果:

+---------+---------------+--------+-------------------------+
| mdindex | mmatter       | mdline | mddesc                  |
+---------+---------------+--------+-------------------------+
| 142     | X30539.000021 | 1      | Avocet, pied            |
+---------+---------------+--------+-------------------------+
| 143     | X30539.000021 | 2      | Margay                  |
+---------+---------------+--------+-------------------------+
| 111     | X30820.004199 | 1      | African buffalo         |
+---------+---------------+--------+-------------------------+
| 112     | X30820.004199 | 2      | Siskin, pine            |
+---------+---------------+--------+-------------------------+
| 113     | X30820.004199 | 3      | African jacana          |
+---------+---------------+--------+-------------------------+
| 114     | X30820.0042   | 2      | Caracara, yellow-headed |
+---------+---------------+--------+-------------------------+
| 115     | X30820.0042   | 3      | Whip-tailed wallaby     |
+---------+---------------+--------+-------------------------+
| 116     | X30820.0042   | 4      | Greater rhea            |
+---------+---------------+--------+-------------------------+
| 120     | X30820.004202 | 1      | Nuthatch, red-breasted  |
+---------+---------------+--------+-------------------------+
| 121     | X30820.004202 | 2      | Arctic tern             |
+---------+---------------+--------+-------------------------+
| 122     | X30820.004202 | 3      | Tyrant flycatcher       |
+---------+---------------+--------+-------------------------+
| 123     | X30820.004203 | 1      | Plover, three-banded    |
+---------+---------------+--------+-------------------------+
| 124     | X30820.004203 | 2      | Tortoise, radiated      |
+---------+---------------+--------+-------------------------+
| 129     | X30820.004204 | 2      | Laughing dove           |
+---------+---------------+--------+-------------------------+
| 130     | X30820.004204 | 3      | Iguana, marine          |
+---------+---------------+--------+-------------------------+

这是我要返回的数据,来自上述结果集:

+-----+---------------+---+-------------------------+
| 114 | X30820.0042   | 2 | Caracara, yellow-headed |
+-----+---------------+---+-------------------------+
| 115 | X30820.0042   | 3 | Whip-tailed wallaby     |
+-----+---------------+---+-------------------------+
| 116 | X30820.0042   | 4 | Greater rhea            |
+-----+---------------+---+-------------------------+
| 129 | X30820.004204 | 2 | Laughing dove           |
+-----+---------------+---+-------------------------+
| 130 | X30820.004204 | 3 | Iguana, marine          |
+-----+---------------+---+-------------------------+

请注意,此结果集中的行没有小于 2 的 mdline 值。

我试过了,但出现语法错误,可能是因为嵌套了太多查询?

SELECT * FROM  
    ( SELECT top 1000
      [mdindex]
          ,[mmatter]
          ,[mdline]
          ,[mddesc]
    FROM [desc]
    WHERE [mmatter] IN (
        SELECT [mmatter]
        FROM [desc]
        GROUP BY [mmatter]
         HAVING COUNT(distinct [mdline]) > 1
        )
        order by mmatter
    )
WHERE mdline not like '1'

【问题讨论】:

样本数据最好使用DDL + DML。请edit您的问题包括它。更多详情,read this. 另外,你的问题不清楚。 更新帖子以包含 SQL Fiddle 链接。我不确定你怎么认为它不清楚。 这样更好。否决票已撤回。 【参考方案1】:

您可以通过在 where 子句中添加 not exists 来获得所需的结果:

SELECT [mdindex]
    ,[mmatter]
    ,[mdline]
    ,[mddesc]
FROM [table1] t0
WHERE [mmatter] IN (
    SELECT [mmatter]
    FROM [table1]
    GROUP BY [mmatter]
    HAVING COUNT(distinct [mdline]) > 1
)
AND NOT EXISTS
(
    SELECT 1
    FROM [table1] t1
    WHERE t0.mmatter = t1.mmatter
    AND t0.mdline = 1
)
ORDER BY mmatter

【讨论】:

【参考方案2】:

我会使用 existsnot exists

select t1.*
from table1 t1
where not exists (select 1 
                  from table1 t2
                  where t2.mmatter = t1.mmatter and t2.mdline = 1 
                  ) and
       exists (select 1 
               from table1 t2
               where t2.mmatter = t1.mmatter and t2.mdline <> t1.mdline
               );

【讨论】:

以上是关于基于数字过滤结果集的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Django 中另一个查询集的结果过滤查询集?

查询集

Django查询集QuerySet及两大特性

如何从 jQuery Datatable 中获取过滤后的数据结果集

使用过滤取结果集

Django中模型