简体   繁体   English

MYSQL:SELECT DISTINCT仅显示唯一的行

[英]MYSQL: SELECT DISTINCT only show rows which are unique

I only want to see the rows where the column 'Stadt'(=City) isn´t equal to 'Hauptstadt'(=Capital). 我只想查看“ Stadt”(=城市)列不等于“ Hauptstadt”(= Capital)的行。 my table looks like: 我的桌子看起来像:

+----------------------+-------------------+--------------------------+---------------+
| Name                 | Stadt             | Land                     | Hauptstadt    |
+----------------------+-------------------+--------------------------+---------------+
| Kunstmuseum          | Bern              | Schweiz                  | Bern          |
| Musée Picasso        | Paris             | Frankreich               | Paris         |
| Museum Ludwig        | Köln              | Deutschland              | Berlin        |
| Museum of Modern Art | New York          | United States of America | Washington DC |
| Städel               | Frankfurt am Main | Deutschland              | Berlin        |
+----------------------+-------------------+--------------------------+---------------+

I only want to see(desired output): 我只想看(期望的输出):

+----------------------+-------------------+--------------------------+---------------+
| Name                 | Stadt             | Land                     | Hauptstadt    |
+----------------------+-------------------+--------------------------+---------------+
| Museum Ludwig        | Köln              | Deutschland              | Berlin        |
| Museum of Modern Art | New York          | United States of America | Washington DC |
| Städel               | Frankfurt am Main | Deutschland              | Berlin        |
+----------------------+-------------------+--------------------------+---------------+

I tried it this: 我尝试了这个:

SELECT DISTINCT Name, Stadt, Land ,Hauptstadt 
FROM Museum 
GROUP BY 'Name', 'Stadt', 'Land', 'Hauptstadt' 
ORDER BY Name;

Output: 输出:

+----------------------+----------+--------------------------+---------------+
| Name                 | Stadt    | Land                     | Hauptstadt    |
+----------------------+----------+--------------------------+---------------+
| Museum of Modern Art | New York | United States of America | Washington DC |
+----------------------+----------+--------------------------+---------------+

How should my SELECT look like to get my desired ouput? 我的SELECT应该如何获得期望的输出?

Thanks for your help! 谢谢你的帮助!

The single quotes in the group by are messing you up. group by的单引号使您感到困惑。 First, only use single quotes for string and date constants. 首先,仅对字符串和日期常量使用单引号。 Second, you almost never need group by with select distinct . 其次,您几乎再也不需要group by select distinct group by Third, you need a where clause for your condition. 第三,您需要一个满足条件的where子句。 So try this: 所以试试这个:

SELECT DISTINCT Name, Stadt, Land ,Hauptstadt 
FROM Museum 
WHERE Stadt <> Hauptstadt
ORDER BY Name;

我们需要按名称分组,如果相等或不相等,则只比较两列

SELECT * FROM Museum WHERE Stadt != Hauptstadt GROUP BY Name;

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

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