我如何才能从MySQL中的一个字段中仅选择第一个不同的匹配项? [英] How can I select only the first distinct match from a field in MySQL?

查看:104
本文介绍了我如何才能从MySQL中的一个字段中仅选择第一个不同的匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的表:



<$>我怎样才能返回MySQL中第一个字段的匹配? p $ p> 名字哈希
----------------
安娜ABC
倒钩DEF
Charlie GHI
Anna JKL $ b $ Andrea MNO

My Query(for %An%):

  SELECT DISTINCT(name)as name,hash FROM my_table WHERE name LIKE'%An%'; 

返回:

 名称hash 
----------------
安娜ABC
安娜JKL
安德莉亚MNO

而不是:(我之后的结果)

 名字hash 
----------------
Anna ABC
Andrea MNO

我如何才能得到每个不同名称的第一个匹配项?



我想返回第一个 Anna ,跳过第二个(和任何后续匹配),但仍然得到 Andrea (以及任何其他不同的匹配项,如 Andrew Anthony )。

解决方案

DISTINCT 不会以这种方式工作,这些值必须在返回的所有列中不同。

您可以在散列函数 GROUP BY名称上使用聚合函数它将返回一个 hash 值每个名称

  SELECT name,min(hash)hash 
FROM my_table
WHERE名称LIKE'%An%'
GROUP BY名称;

请参阅 SQL小提琴演示


$ b 注意:使用< c $ c> GROUP BY 将确保您将始终返回散列列的期望值。当您不 GROUP BY 或汇总 SELECT 列表中的项目时,您可能会返回意外的结果。 (请参阅 MySQL扩展至 GROUP BY



从MySQL文档:


MySQL扩展了GROUP BY的使用,以便选择列表可以引用未在GROUP BY子句中命名的非聚合列。 ...您可以使用此功能通过避免不必要的列排序和分组来获得更好的性能。但是,这非常有用,因为每个未在GROUP BY中命名的非聚合列中的所有值对于每个组都是相同的。服务器可以自由选择每个组中的任何值,因此除非它们相同,否则所选值是不确定的。此外,每个组的值的选择不能通过添加ORDER BY子句来影响。结果集的排序发生在选择了值之后,而ORDER BY不会影响服务器选择的值。



How can I only return the first distinct match of a field in MySQL?

My Table:

name     hash
----------------
Anna     ABC
Barb     DEF
Charlie  GHI
Anna     JKL
Andrea   MNO

My Query (for %An%) :

SELECT DISTINCT(name) as name, hash FROM my_table WHERE name LIKE '%An%';

This returns:

name     hash
----------------
Anna     ABC
Anna     JKL
Andrea   MNO

Instead of: (the result I'm after)

name     hash
----------------
Anna     ABC
Andrea   MNO

How can I get only the first match of each distinct name?

I want to return the first Anna, skip the second (and any subsequent matches), but still get Andrea (and any further distinct matches, like Andrew or Anthony).

解决方案

DISTINCT does not work that way, the values must be distinct across all columns being returned.

You can always use an aggregate function on the hash function and GROUP BY name which will return one hash value for each name:

SELECT name, min(hash) hash
FROM my_table 
WHERE name LIKE '%An%' 
GROUP BY name;

See SQL Fiddle with Demo.

Note: using the aggregate function with the GROUP BY will make sure that you will always return the expected value for the hash column. When you do not GROUP BY or aggregate the items in the SELECT list, you might return unexpected results. (see MySQL Extensions to GROUP BY)

From the MySQL Docs:

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. ... You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values the server chooses.

这篇关于我如何才能从MySQL中的一个字段中仅选择第一个不同的匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆