gpt4 book ai didi

mysql - SQL 查询中的问题

转载 作者:行者123 更新时间:2023-11-29 08:20:03 24 4
gpt4 key购买 nike

我编写了一个查询,用于从表中的一列中获取值,并根据 where 子句将它们表示在数据 GridView 中的两列中。以下是查询,

Select ElectricityMachinePanelDetails.MachineDescription AS "Machine Name",
case when (ReadingCategoryID = 'RC001' AND ReadingTypesDetails.ReadingTypeID = 'RT001')
then ReadingValue
end as [Last Day Meter Reading],
case when (ReadingCategoryID = 'RC001' AND ReadingTypesDetails.ReadingTypeID = 'RT002')
then ReadingValue
end as [Current Day Meter Reading]
from ElectricityDailyMeterReadingDetails
INNER JOIN ElectricityMachinePanelDetails on ElectricityMachinePanelDetails.MachinePanelID = ElectricityDailyMeterReadingDetails.MachinePanelID
INNER JOIN ReadingTypesDetails ON ElectricityDailyMeterReadingDetails.ReadingTypeID = ReadingTypesDetails.ReadingTypeID
WHERE ReadingCategoryID = 'RC001' AND ReadingTypesDetails.ReadingTypeID = 'RT001'

根据读取类型 ID,列的值会发生变化......例如

如果我使用ReadingTypesDetails.ReadingTypeID = 'RT002'

Machine Name           Last Day Meter Reading    Current Day Meter Reading                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Transformer 01 NULL 60.000000
Transformer 02 NULL 60.000000
Transformer 03 NULL 80.000000

如果我使用ReadingTypesDetails.ReadingTypeID = 'RT001'

Machine Name           Last Day Meter Reading    Current Day Meter Reading                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Transformer 01 52.000000 NULL
Transformer 02 55.000000 NULL
Transformer 03 70.000000 NULL

*总是有一列显示为 NULL * ..有人可以帮我解决这个问题吗?

最佳答案

试试这个:

Select  ElectricityMachinePanelDetails.MachineDescription AS "Machine Name",
case when (ReadingTypesDetails.ReadingTypeID = 'RT001') then ReadingValue
end as [Last Day Meter Reading],
case when (ReadingTypesDetails.ReadingTypeID = 'RT002') then ReadingValue
end as [Current Day Meter Reading]
from ElectricityDailyMeterReadingDetails
INNER JOIN ElectricityMachinePanelDetails on ElectricityMachinePanelDetails.MachinePanelID = ElectricityDailyMeterReadingDetails.MachinePanelID
INNER JOIN ReadingTypesDetails ON ElectricityDailyMeterReadingDetails.ReadingTypeID = ReadingTypesDetails.ReadingTypeID
WHERE ReadingCategoryID = 'RC001'

关于mysql - SQL 查询中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19649681/

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