私は他の答えから関数を借りたことを認めgroup_concat()
ます:)
ドキュメントからこの段落を読んだ後:
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(*)
、2番目の制限と同じ範囲である必要があります(つまり3
、最初の例と5
2番目の例)。そして、2番目の制限は常に0
示されているように始まります。
私はこれに取り組むことを楽しんだ、これが役立つことを願っている。