sql查询得到一个重复数据表的n个元素

sqxo8psd  于 2021-06-20  发布在  Mysql
关注(0)|答案(8)|浏览(234)

我有一个以这种格式返回数据的查询 `| Name | SomeData | MoreStuff | |--------|

ie3xauqp

ie3xauqp1#

这将获得前3个资产的行,而不必使用自联接:
sql小提琴
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

结果:

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

ssgvzors3#

这将为每个资产提供最多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小提琴演示!

mccptt67

mccptt674#

-----| | 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个不同的资产,但也需要每行的数据。一个“限制”在这里是行不通的,一个“分组”也行不通。 我还有别的选择吗? -----编辑---- 例如,如果我需要3个不同的资产,那么输出应该是| Name | SomeData | MoreStuff | |--------|

lb3vh1jj

lb3vh1jj5#

试试这个:

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

tquggr8v6#

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

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

演示

zpjtge22

zpjtge227#

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

相关问题