简体   繁体   English

如何为下表编写SQL查询?

[英]How to write a SQL-Query for the following table?

My table is defined like this: 我的表定义如下:

Name is a string and property too. Name也是一个字符串和属性。

ID | Name | Property

An example for data in this table is this: 此表中数据的示例如下:

ID | Name | Property
1    Peter  Newsletter
2    Paul   Register
3    Peter  Register
4    Shaun  Newsletter
5    Steve  Register

Now I like to query all people that have the property newsletter and register. 现在,我想查询所有拥有房产新闻通讯并注册的人。 As a result I should get Peter, because he has both property's. 因此,我应该得到彼得,因为他有两个属性。

So the resulting table should be like: 所以结果表应该是这样的:

ID | Name | Property
1    Peter  Newsletter
3    Peter  Register

So everything I try to query is which person has both property's newsletter and register. 所以我试图询问的是哪个人都有财产的通讯和注册。

Here is one method: 这是一种方法:

select t.*
from table t
where exists (select 1
              from table t2
              where t2.name = t.name and t2.property = 'NewsLetter'
             ) and
      exists (select 1
              from table t2
              where t2.name = t.name and t2.property = 'Register'
             );

If you just want the list of names, perhaps with id s, I would do that as: 如果你只想要名字列表,也许是id s,我会这样做:

select t.name
from table t
where t2.property in ('NewsLetter', 'Register')
group by t.name
having count(distinct property) = 2;

How you get the list of id s depends on your database, something like listagg() or group_concat() or string_agg() . 如何获取id列表取决于您的数据库,例如listagg()group_concat()string_agg()

An alternative, pretty much on the same lines as Gordon's solution, but without using EXISTS : 另一种选择,与Gordon的解决方案几乎相同,但不使用EXISTS

select * from tablename
where name in (select name from tablename where property = 'Newsletter')
and name in (select name from tablename where property = 'Register')

It's hard to be sure without knowing more about the data. 如果不了解更多数据,很难确定。 Given the exact requirements that you gave to us, this will give the results you showed: 鉴于您给我们的确切要求,这将给出您显示的结果:

WITH multprop (multName) AS (
   SELECT NAME FROM myTable
       WHERE Property IN('Newsletter','Register')
       GROUP BY NAME
       HAVING count(*)>1 )
select id, Name, Property
 from multprop inner join myTable
      on multName = Name

But minor differences in your requirements will mess things up. 但是你的要求中的细微差别会使事情变得混乱。 For example, will there ever be Property values other than the two you listed? 例如,除了您列出的两个之外, 是否会有属性值? Or can a Name show up multiple times with the same Property? 或者名称可以使用相同的属性多次显示?

EDIT: The added WHERE clause limits rows in the CTE to the requested specific set of Property values. 编辑:添加的WHERE子句将CTE中的行限制为请求的特定Property值集。 This is from the more detailed requirements in comments. 这来自评论中更详细的要求。

One more way: 还有一种方法:

SELECT * FROM T as T1
WHERE Property IN ('Newsletter','Register')
      AND EXISTS (SELECT * FROM T 
                      WHERE Name=T1.Name 
                            and Property IN ('Newsletter','Register')
                            and Property <> T1.Property
                 )

SQLFiddle demo SQLFiddle演示

Another one, for the record 另一个,为记录

WITH cteHasBoth
 as (select Name
       from MyTable
       where Property in ('Newsletter', 'Register')
       group by Name
       having count(*) = 2)
 select ID, Name
  from MyTable
  where name in (select Name from cteHasBoth)

This would require only two sacns through the table. 通过桌面只需要两个圣杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM