MySQL 中的聚合函数:将值聚合到列表中(类似于 LISTAGG) Oracle)
查询:
I need function, that returns list of strings. I have data in table like this: Id MyString ------------------------ 1 First 2 Second 3 Third 4 Fourth I need function like this (something like this works in oracle): select LISTAGG(MyString, ', ') as myList where id <p><strong>答案:</strong></p><p>MySQL 提供了 GROUP_CONCAT() 函数这个目的。它将值聚合到以逗号分隔的列表中。</p><p><strong>使用 GROUP_CONCAT() 查询:</strong></p><pre class="brush:php;toolbar:false">select group_concat(MyString separator ', ') as myList from table where id <p>此查询将返回以逗号分隔的字符串列表,对于 id 小于 4 的所有行。“myList”列将包含聚合的结果。</p><p><strong>注意:</strong> GROUP_CONCAT() 函数允许在必要时按多列进行分组。例如:</p><pre class="brush:php;toolbar:false">select group_concat(MyString separator ', ') as myList from table where id <p>此查询将按 another_column 列对结果进行分组,并为每个组创建一个字符串列表。</p>
以上是MySQL中如何将字符串聚合到列表中(类似于Oracle的LISTAGG)?的详细内容。更多信息请关注PHP中文网其他相关文章!