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 |
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;
-----| | 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 | |--------|
-----| | 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 |`
8条答案
按热度按时间ie3xauqp1#
这将获得前3个资产的行,而不必使用自联接:
sql小提琴
mysql 5.6架构设置:
查询1:
结果:
brvekthn2#
ssgvzors3#
这将为每个资产提供最多3行。您可以将3更改为任意数字,然后您将得到每个资产的那么多行。
sql小提琴演示!
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 | |--------|lb3vh1jj5#
试试这个:
tquggr8v6#
您可以通过对同一个表执行连接来获得所需的结果集,但行数有限,如
演示
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 |`
pod7payv8#