mysql 展平各列 MYSQL 展平表查詢

2021-10-18 06:47:33 字數 2628 閱讀 5955

我對下面的查詢有一些問題,我不確定如何修改它以給我所需的輸出,詳情如下:

select listup.nodenumber as node, listup.station as extension,

case when voiceserver = 1 then keydescription else '' end as 'key1',

case when voiceserver = 2 then keydescription else '' end as 'key2',

case when voiceserver = 3 then keydescription else '' end as 'key3',

case when voiceserver = 4 then keydescription else '' end as 'key4',

case when voiceserver = 5 then keydescription else '' end as 'key5',

case when voiceserver = 6 then keydescription else '' end as 'key6',

case when voiceserver = 7 then keydescription else '' end as 'key7',

case when voiceserver = 8 then keydescription else '' end as 'key8',

case when voiceserver = 9 then keydescription else '' end as 'key9',

case when voiceserver = 10 then keydescription else '' end as 'key10'

from listup

order by listup.nodenumber, listup.station;

輸出如下:

node extension key1 key2 key3 etc.

n100 14311 14311

n100 14311 14308

n100 14311 14309

n100 14311 14314

n100 14311 14412

n100 14311 14535

n100 14311 14316

n100 14311 14456

n100 14312 14312

n100 14312 14442

n100 14312 14311

n100 14312 14314

n100 14312 14456

n100 14312 14309

n100 14312 14308

我想把它全部出現在一條線上,所以這樣扁平化了……

例如.n100 14311 14311 14308 14309 14314 14412 14535 14316 14456

n100 14312 14312 14442 14311 14314 14456 14309 14308

最佳答案 您需要定義group by子句,並使用max聚合來獲取所需的輸出:

select listup.nodenumber as node, listup.station as extension,

max(case when voiceserver = 1 then keydescription else null end) as 'key1',

max(case when voiceserver = 2 then keydescription else null end) as 'key2',

max(case when voiceserver = 3 then keydescription else null end) as 'key3',

max(case when voiceserver = 4 then keydescription else null end) as 'key4',

max(case when voiceserver = 5 then keydescription else null end) as 'key5',

max(case when voiceserver = 6 then keydescription else null end) as 'key6',

max(case when voiceserver = 7 then keydescription else null end) as 'key7',

max(case when voiceserver = 8 then keydescription else null end) as 'key8',

max(case when voiceserver = 9 then keydescription else null end) as 'key9',

max(case when voiceserver = 10 then keydescription else null end) as 'key10'

from listup

group by listup.nodenumber as node, listup.station as extension

order by listup.nodenumber, listup.station

null優於零長度字串.

mysql 展平各列 MYSQL 展平表查詢

我對下面的查詢有一些問題,我不確定如何修改它以給我所需的輸出,詳情如下 select listup.nodenumber as node,listup.station as extension,case when voiceserver 1 then keydescription else end ...

sql 陣列三 展平陣列

展平陣列 如需將 array 轉換為一組行 即執行展平操作 請使用 unnest 運算子。unnest 獲取乙個 array,然後返回乙個表,array 中的每個元素均佔該錶的一行。由於 unnest 破壞了 array 元素的順序,您可能需要恢復表中的順序。為此,請使用可選的 with offse...

Mesh引數化方法 Mesh展平

mesh引數化或者mesh展平是圖形學裡面廣泛用於製作貼圖的乙個步驟,通常英文稱之為mesh parameterization,也被通俗的稱為mesh flattenning。基本概念是乙個三維的離散網格對映到二維平面網格的過程。為了保證後續應用的有效性,網格的引數化通常需要滿足一些特定的性質,如三...