2

我尝试构建的查询将使用tableA中的以下列:

  • 姓名
  • 描述

查询应提供以下过滤器:

  • tableA中选择不重复的名称
  • 仅选择没有重复描述的名称
  • 选择不包含字符串的描述:“任何”或“已删除”
  • 按名称排序

这是我目前的陈述,除了重复的描述之外,它给了我一切。如何将此添加到我当前的声明中?

选择

 DISTINCT name FROM tableA

 WHERE description NOT LIKE 'Any%'

 AND description NOT LIKE 'Deleted%'

按名称订购

4

4 回答 4

1

您应该尝试使用自联接。
使用左侧表格过滤以“任何”或“已删除”开头的描述。由于您要消除具有重复描述的行,因此您只选择右侧具有相同描述但找不到不同名称的匹配行的结果:

SELECT
    leftSide.name,
    leftSide.description
FROM
    tableA as leftSide
    LEFT JOIN tableA as rightSide ON (
        leftSide.description = rightSide.description AND
        leftSide.name <> rightSide.name
    )
WHERE
    rightSide.description IS NULL AND
    leftSide.description NOT LIKE 'Any%' AND
    leftSide.description NOT LIKE 'Deleted%'
GROUP BY
    leftSide.name, leftSide.description
ORDER BY
    leftSide.name
于 2012-11-29T01:51:04.103 回答
0

SELECT t.name FROM tableA AS t GROUP BY t.name HAVING COUNT(*)=1 AND t.description NOT LIKE 'Any%' AND t.description NOT LIKE 'Deleted%' ORDER BY t.name;

于 2012-11-29T07:24:31.143 回答
0
SELECT

 DISTINCT name FROM tableA

 WHERE description NOT LIKE 'Any%','Deleted%'

ORDER BY name
于 2012-11-29T01:41:32.970 回答
0

这会是您正在寻找的解决方案吗?

参考:SQLFIDDLE

询问:

select distinct name, des
from data
where des not like '%any%'
and des not like '%delete%'
order by name ASC;

结果:

NAME    DES

mary    yeah all or none
tim     yeah all or none
you     did you see he is in
于 2012-11-29T01:42:03.687 回答