首页 > 解决方案 > SQL查询获取具有重复数据的表的N个元素

问题描述

我有一个以这种格式返回数据的查询

| Name | SomeData | MoreStuff | |--------|-------------|---------------| | asset1 | I need this | And also this | | asset1 | I need this | And also this | | asset1 | I need this | And also this | | asset2 | I need this | And also this | | asset2 | I need this | And also this | | asset3 | I need this | And also this | | asset3 | I need this | And also this | | asset3 | I need this | And also this | | asset4 | I need this | And also this | | asset5 | I need this | And also this | | asset5 | I need this | And also this | | ...... | ........... | ............. |

假设我需要 20 个不同的资产,还需要每一行的数据。这里的“LIMIT”不起作用,“GROUP BY”也不起作用。

我还有什么其他选择?

- - - 编辑 - -

例如,如果我需要 3 个不同的资产,则输出应该是

| Name | SomeData | MoreStuff | |--------|-------------|---------------| | asset1 | I need this | And also this | | asset1 | I need this | And also this | | asset1 | I need this | And also this | | asset2 | I need this | And also this | | asset2 | I need this | And also this | | asset3 | I need this | And also this | | asset3 | I need this | And also this | | asset3 | I need this | And also this |

标签: mysqlsql

解决方案


您可以通过对同一个表进行联接来获得所需的结果集,但行数有限,例如

select a.*
from demo a
join (
  select distinct Name
  from demo 
  order by Name
  limit 3
) b on a.Name = b.Name

演示


推荐阅读