简体   繁体   English

SQL:在where子句中不同

[英]SQL: distinct in where clause

I have a query that after execution shows: 我有一个查询,执行后显示:

Name  | LasName | City
-------------------------
John  | Doe     | London
John  | Doe     | Berlin
Martin| Smith   | Paris

Is there anything I can do in the WHERE clause section in order to get just one John Doe, no matter the city? 我可以在WHERE子句部分中做任何事情,以便获得一个John Doe,而不论城市是什么?

The query is humongous that's why I'm not posting it in here. 该查询是巨大的,这就是为什么我不在此发布它。

If there's anything you think I can do, please, post a fake query explaining it, I just need to get the concept. 如果您认为我可以做任何事情,请发布一个伪造的查询解释它,我只需要了解这个概念即可。

Thanks a lot! 非常感谢!

You don't state RDBMS. 您没有声明RDBMS。 One way that should work in all. 一种应该起作用的方法。

SELECT Name,
       LasName,
       MAX(City) AS City
FROM   YourQuery
GROUP  BY Name,
          LasName 

You say you don't care which city. 你说你不在乎哪个城市。 This gives you the last alphabetically. 这给您最后一个字母顺序。

Or an alternative 或替代

SELECT Name,
       LasName,
       City
FROM   (SELECT Name,
               LasName,
               City,
               ROW_NUMBER() OVER (PARTITION BY Name, LasName ORDER BY (SELECT 0)) AS RN
        FROM   YourQuery) T
WHERE  RN = 1 

Assuming you're using MySQL and you want to see all the cities for the same user name on one line, try something like: 假设您使用的是MySQL,并且想在一行上查看同一用户名的所有城市,请尝试以下操作:

select Name, LasName, group_concat(City)
from LargeQuery
group by Name, LasName

You can use the following query (t-sql) 您可以使用以下查询(t-sql)

SELECT Distinct Name,LasName
FROM TableName

Assuming that you are asking to query just one row, irrespective of the city, try this, 假设您要查询的行仅与城市无关,请尝试此操作,

select * from tablename 
where name = 'John'
and lasname = 'Doe'
and rownum = 1;
select * from `TableName` group by Name, LasName

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

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