gpt4 book ai didi

mysql - 自连接表以获取 2 列的所有组合的值

转载 作者:行者123 更新时间:2023-11-29 23:20:37 26 4
gpt4 key购买 nike

我有一个表,其中包含三列idtime_stampvm_namecpu_usage

该表没有每个时间戳的所有 vm_name 的值。

但我想要一个包含每个 time_stamp 的每个 vm_name 值的结果。

我的表有 4 个唯一的虚拟机和 20 个唯一的时间戳。 该表包含 50 行。

我想要返回 4 X 20 = 80 行,如果 vm_name 和 time_stamp 的组合尚不存在,它将为 cpu 使用率分配 0 值。

下面的尝试返回 162 行,其中我没有看到任何不存在的 cpuusage 值的空值。

select t1.timestamp,t2.timestamp,t1.vm_name,t2.vm_name,t1.cpuUsage, t2.cpuUsage from       `VM_monitoring_5min t1` 
right join VM_monitoring_5min t2
on t1.timestamp = t2.timestamp

这是 SQL Fiddle http://sqlfiddle.com/#!2/998e0/1

最佳答案

select distinct n.vm_name
, c.timestamp
, coalesce(t1.cpuUsage,0)
from VM_monitoring_5min n
join VM_monitoring_5min c
left
join VM_monitoring_5min t1
On t1.vm_name = n.vm_name
and t1.timestamp = c.timestamp

http://sqlfiddle.com/#!2/998e0/3

关于mysql - 自连接表以获取 2 列的所有组合的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27340125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com