LoginSignup
0
1

More than 5 years have passed since last update.

複数カラムの内容をカンマ区切りで連結、空欄は飛ばすSQL

Posted at
SELECT CONCAT_WS(',', NULLIF(`c1`,''),NULLIF(`c2`,''),NULLIF(`c3`,''),NULLIF(`c4`,''),NULLIF(`c5`,''),NULLIF(`c6`,''))  
FROM `tb`

下記のような結果が出る

a,b,c
d,e
f,g,h,i,j
k

カラムにNULLを可にしていて、空欄とNULLを分けている場合はNULLIFを使わずにカラム名だけにする

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1