选择不同的列以及 MySQL 中的一些其他列

发布于 2024-12-21 21:59:47 字数 1116 浏览 0 评论 0原文

我似乎无法为以下(可能是一个古老的)问题找到合适的解决方案,因此希望有人能够阐明一些观点。我需要在 mySQL 中返回 1 个不同的列以及其他非不同的列。

我在 mySQL 中有下表:

id      name       destination     rating     country
----------------------------------------------------
1       James      Barbados        5          WI
2       Andrew     Antigua         6          WI
3       James      Barbados        3          WI
4       Declan     Trinidad        2          WI
5       Steve      Barbados        4          WI
6       Declan     Trinidad        3          WI

我希望 SQL 语句返回不同的名称以及目的地、基于国家/地区的评级。

id      name       destination     rating     country
----------------------------------------------------
1       James      Barbados        5          WI
2       Andrew     Antigua         6          WI
4       Declan     Trinidad        2          WI
5       Steve      Barbados        4          WI

正如您所看到的,詹姆斯和德克兰的评分不同,但名字相同,因此他们只返回一次。

以下查询返回所有行,因为评级不同。无论如何我可以返回上面的结果集吗?

SELECT (distinct name), destination, rating 
  FROM table 
 WHERE country = 'WI' 
 ORDER BY id

I can't seem to find a suitable solution for the following (probably an age old) problem so hoping someone can shed some light. I need to return 1 distinct column along with other non distinct columns in mySQL.

I have the following table in mySQL:

id      name       destination     rating     country
----------------------------------------------------
1       James      Barbados        5          WI
2       Andrew     Antigua         6          WI
3       James      Barbados        3          WI
4       Declan     Trinidad        2          WI
5       Steve      Barbados        4          WI
6       Declan     Trinidad        3          WI

I would like SQL statement to return the DISTINCT name along with the destination, rating based on country.

id      name       destination     rating     country
----------------------------------------------------
1       James      Barbados        5          WI
2       Andrew     Antigua         6          WI
4       Declan     Trinidad        2          WI
5       Steve      Barbados        4          WI

As you can see, James and Declan have different ratings, but the same name, so they are returned only once.

The following query returns all rows because the ratings are different. Is there anyway I can return the above result set?

SELECT (distinct name), destination, rating 
  FROM table 
 WHERE country = 'WI' 
 ORDER BY id

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嗳卜坏 2024-12-28 21:59:47

使用子查询,您可以获取每个名称的最高 id,然后根据该值选择其余行:

SELECT * FROM table
WHERE id IN (
  SELECT MAX(id) FROM table GROUP BY name
)

如果您愿意,可以使用 MIN(id)获取每个名称的第一条记录而不是最后一条记录。

也可以通过针对子查询的INNER JOIN 来完成。为此,性能应该相似,有时您需要连接子查询中的列。

SELECT
  table.*
FROM 
  table
  INNER JOIN (
    SELECT MAX(id) AS id FROM table GROUP BY name
  ) maxid ON table.id = maxid.id

Using a subquery, you can get the highest id for each name, then select the rest of the rows based on that:

SELECT * FROM table
WHERE id IN (
  SELECT MAX(id) FROM table GROUP BY name
)

If you'd prefer, use MIN(id) to get the first record for each name instead of the last.

It can also be done with an INNER JOIN against the subquery. For this purpose the performance should be similar, and sometimes you need to join on two columns from the subquery.

SELECT
  table.*
FROM 
  table
  INNER JOIN (
    SELECT MAX(id) AS id FROM table GROUP BY name
  ) maxid ON table.id = maxid.id
梦途 2024-12-28 21:59:47

问题在于,distinct 作用于整个返回集,而不仅仅是第一个字段。否则MySQL将不知道要返回什么记录。因此,您需要某种关于评级的组函数,无论是 MAX、MIN、GROUP_CONCAT、AVG 还是其他几个函数。

迈克尔已经发布了一个很好的答案,所以我不会重新编写查询。

The problem is that distinct works across the entire return set and not just the first field. Otherwise MySQL wouldn't know what record to return. So, you want to have some sort of group function on rating, whether MAX, MIN, GROUP_CONCAT, AVG, or several other functions.

Michael has already posted a good answer, so I'm not going to re-write the query.

赏烟花じ飞满天 2024-12-28 21:59:47

我同意@rcdmk。使用 DEPENDENT 子查询会降低性能,如果您已经索引了 country 字段并且只有几行会到达服务器,那么 GROUP BY 似乎更合适。通过 @rcdmk 重写查询 giben ,我添加了 ORDER BY NULL 子句来抑制 GROUP BY 的隐式排序,使其更快一些:

SELECT MIN(id) as id, name, destination as rating, country 
FROM table WHERE country = 'WI' 
GROUP BY name, destination ORDER BY NULL

I agree with @rcdmk . Using a DEPENDENT subquery can kill performance, GROUP BY seems more suitable provided that you have already INDEXed the country field and only a few rows will reach the server. Rewriting the query giben by @rcdmk , I added the ORDER BY NULL clause to suppress the implicit ordering by GROUP BY, to make it a little faster:

SELECT MIN(id) as id, name, destination as rating, country 
FROM table WHERE country = 'WI' 
GROUP BY name, destination ORDER BY NULL
银河中√捞星星 2024-12-28 21:59:47

您可以执行 GROUP BY 子句:

SELECT MIN(id) AS id, name, destination, AVG(rating) AS rating, country
FROM TABLE_NAME
GROUP BY name, destination, country

此查询在大型数据集中比子查询替代方案执行得更好,并且也更易于阅读。

You can do a GROUP BY clause:

SELECT MIN(id) AS id, name, destination, AVG(rating) AS rating, country
FROM TABLE_NAME
GROUP BY name, destination, country

This query would perform better in large datasets than the subquery alternatives and it can be easier to read as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文