SQL 将 2 个表合并为一列,以逗号分隔
SQL combine 2 tables in one column sepataring by comma
你好,我的 SQL 查询有问题,我想将 2 个表的 5 列组合起来并用逗号分隔,下面是 post 图片表的外观。
在例子中:
名称:长度,j1:mm,j2:cm,j3:NULL,j4:m
名称:大小,j1:NULL,j2:NULL,j3:NULL,j4:NULL;
查询后的结果我想要这样的:
如果在行中我发现 NULL,我们应该 return" - "
我尝试了 Concat,但我不知道如何 return - 如果我找到空白行
我想你想要 coalesce()
和 concat_ws()
。像这样:
select concat_ws(',',
coalesce(name, '-'),
coalesce(j1, '-'),
coalesce(j2, '-'),
coalesce(j3, '-'),
coalesce(j4, '-')
)
from t1 left join
t2
using (t2_id)
你好,我的 SQL 查询有问题,我想将 2 个表的 5 列组合起来并用逗号分隔,下面是 post 图片表的外观。
在例子中:
名称:长度,j1:mm,j2:cm,j3:NULL,j4:m
名称:大小,j1:NULL,j2:NULL,j3:NULL,j4:NULL;
查询后的结果我想要这样的:
如果在行中我发现 NULL,我们应该 return" - "
我尝试了 Concat,但我不知道如何 return - 如果我找到空白行
我想你想要 coalesce()
和 concat_ws()
。像这样:
select concat_ws(',',
coalesce(name, '-'),
coalesce(j1, '-'),
coalesce(j2, '-'),
coalesce(j3, '-'),
coalesce(j4, '-')
)
from t1 left join
t2
using (t2_id)