查询一行显示数据

时间:2017-11-15 07:20:38

标签: mysql

如何获取在行上显示的值Total,free,在一行而不是三行下使用。

SELECT 
h.host as 'Server', 
i.key_,max(x.clock), 
case when i.key_ like '%Total%' then x.value end as 'Total',
case when i.key_ like '%free%' then x.value end as 'free',
case when i.key_ like '%used%' then x.value end as 'used',
x.itemid, 
x.value
FROM `history_uint` as x
left join items as i on x.itemid = i.itemid
left join hosts as h on h.hostid = i.hostid
Where i.key_ LIKE 'vfs.fs.size%'
group by h.host

这是我此时收到的输出 [收到1

Wanted

1 个答案:

答案 0 :(得分:0)

选择中的i.key_是没有意义的 - 它将是不确定的,否则我需要条件聚合,或许像这样。

SELECT 
 h.host as 'Server', 
 max(x.clock), 
 sum(case when i.key_ like '%Total%' then x.value else 0 end) as 'Total',
 sum(case when i.key_ like '%free%' then x.value else 0 end) as 'free',
 sum(case when i.key_ like '%used%' then x.value else 0 end) as 'used',
 x.itemid, 
 x.value
 FROM `history_uint` as x
 left join items as i on x.itemid = i.itemid
 left join hosts as h on h.hostid = i.hostid
 Where i.key_ LIKE 'vfs.fs.size%'
 group by h.host