mysql - 如何在查询后合并字段,我知道 CONCAT,但不是这种方式

标签 mysql sql sql-server oracle

<分区>

我知道合并字段的方法

  • MySQL:CONCAT( )
  • 甲骨文:CONCAT( )||
  • SQL 服务器:`+

但是... 我想在查询后合并,这可能吗?

最佳答案

给你:

MySQL 使用 group_concat:

select a.name,
    a.opcode,
    group_concat(month order by b.pk separator ', ') as months
from tablea a
join tableb b on a.opcode = b.opcode
group by a.name, a.opcode;

Oracle 使用 listagg:

select a.name,
    a.opcode,
    listagg(month,', ') within group (order by b.pk) as months
from tablea a
join tableb b on a.opcode = b.opcode
group by a.name, a.opcode;

SQL Server 使用 for xml pathstuff:

select a.*,
    stuff((
        select ', ' + month from tableb b
        where a.opcode = b.opcode
        order by pk 
        for xml path(''), type
        ).value('(./text())[1]', 'varchar(max)')
    , 1, 2, '') as months
from tablea a;

关于mysql - 如何在查询后合并字段,我知道 CONCAT,但不是这种方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43170333/

相关文章:

sql - 对 CASE WHEN inside INSERT 的 SQL 语句感到困惑

sql - TSQL 替换为长度约束

php - 对大量单词进行检查的最有效方法

sql - 查询优化不先执行内部查询

python - Alembic,如何更改 ForeigenKey 列

SQL AND OR 评估不同的结果

sql - 如何合并 BigQuery 中多行的 NULL?

sql-server - SQL Server Management Studio 架构 View

mysql 使用关键字表搜索...最佳方法

mysql - 在mySQL中创建新表时如何将整数数组定义为字段