我承认我从其他答案中借用了group_concat() 函数:)
看完这段话来自docs:
The default behavior for UNION is that duplicate rows are removed from the result.
The optional DISTINCT keyword has no effect other than the default because it also
specifies duplicate-row removal. With the optional ALL keyword, duplicate-row removal
does not occur and the result includes all matching rows from all the SELECT statements.
假设如下表(testdb.test):
ID Name Price
1 Item-A 10
2 Item-A 15
3 Item-A 9.5
4 Item-B 5
5 Item-B 4
6 Item-B 4.5
7 Item-C 50
8 Item-C 55
9 Item-C 40
您可以分页此表的行(9 行)或组(3 组,根据项目的名称)。
如果您想根据项目组对项目进行分页,这应该会有所帮助:
SELECT
name, group_concat(price)
FROM
testdb.test
GROUP BY name
LIMIT 1 , 3
UNION SELECT
name, group_concat(price)
FROM
testdb.test
GROUP BY name
LIMIT 0 , 3; -- Constant 0, range is the same as the first limit's
如果您想根据所有项目对项目进行分页(我认为这不是您所要求的,但以防万一它对其他人有所帮助),这应该会有所帮助:
SELECT
name, price
FROM
testdb.test
LIMIT 1 , 5
UNION SELECT
name, price
FROM
testdb.test
LIMIT 0 , 5; -- Constant 0, range is the same as the first limit's
需要注意的一个非常重要的事情是您必须如何修改限制。第一个限制是您的关键,您可以从您想要的任何限制开始,只要它是 count(*) 但您必须具有与第二个限制相同的范围(即第一个示例中的 3 和 @ 987654330@ 在第二个例子中)。如图所示,第二个限制将始终从0 开始。
我很喜欢这方面的工作,希望对您有所帮助。