1

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?

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!

4

5 回答 5

5

您没有声明 RDBMS。一种应该适用的方法。

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

你说你不在乎哪个城市。这会按字母顺序为您提供最后一个。

或替代方案

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 
于 2013-02-06T12:48:29.770 回答
2

假设您使用的是 MySQL,并且您想在一行上查看相同用户名的所有城市,请尝试以下操作:

select Name, LasName, group_concat(City)
from LargeQuery
group by Name, LasName
于 2013-02-06T12:49:05.093 回答
1

您可以使用以下查询(t-sql)

SELECT Distinct Name,LasName
FROM TableName
于 2013-02-06T12:50:45.867 回答
0
select * from `TableName` group by Name, LasName
于 2013-02-07T14:06:31.430 回答
0

假设您只要求查询一行,不考虑城市,试试这个,

select * from tablename 
where name = 'John'
and lasname = 'Doe'
and rownum = 1;
于 2013-02-06T12:49:58.460 回答