SQL查询以获取具有重复数据的表的N个元素

时间:2018-05-21 10:14:19

标签: mysql sql

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

| 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个不同的资产,还需要每行的数据。 这里的“限制”不起作用,也不适用“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 |

4 个答案:

答案 0 :(得分:1)

您可以通过连接到同一个表但使用

等有限行来获得所需的结果集
select a.*
from demo a
join (
  select distinct Name
  from demo 
  order by Name
  limit 3
) b on a.Name = b.Name

Demo

答案 1 :(得分:1)

这将获得前3个资产的行,而不必使用自联接:

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE table_name (
  Name      VARCHAR(20),
  SomeData  VARCHAR(20),
  MoreStuff VARCHAR(20)
);


INSERT INTO table_name VALUES ( 'asset4', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset5', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset2', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset3', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset1', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset3', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset2', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset1', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset1', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset3', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset5', 'I need this', 'And also this' );

查询1

SELECT Name, SomeData, MoreStuff
FROM   (
  SELECT @asset_num := IF( @prev_name = t.name, @asset_num, @asset_num + 1 ) AS an,
         t.*,
         @prev_name := Name
  FROM   table_name t
         CROSS JOIN
         ( SELECT @prev_name := '', @asset_num := 0 ) r
  ORDER BY Name
) t
WHERE an <= 3

<强> Results

|   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 |

答案 2 :(得分:1)

试试这个:

select *
from TABLE
where Name in (
    select distinct Name 
    from TABLE
    limit 3
)

答案 3 :(得分:0)

这将为每个资产提供最多3行。您可以将3更改为任何数字,然后您将为每个资产获得那么多行。

SELECT Name, SomeData, MoreStuff
FROM (
       SELECT  @name_number := IF(@Name = Name, @name_number + 1, 1) AS name_number, 
               @Name := Name as Name, SomeData, MoreStuff
       FROM 
              (SELECT @name_number := 1) x, 
              (SELECT SomeData, MoreStuff, @Name := Name as Name FROM your_table ORDER BY Name) y
      ) z
WHERE name_number <= 3;

Sql Fiddle Demo Here!