-2

我正在编写 sql 查询,我想在 datagrid 视图中使用同一列两次。

这是我的查询:

SELECT ElectricityMachinePanelDetails.MachineDescription AS "Machine Name",
       ReadingValue AS "Last 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' 

如何ReadingValue在数据网格中使用两次列名。

4

2 回答 2

1

首先,在连接中使用别名而不是表名。所以你的查询可能更具可读性。

select
    empd.MachineDescription as "Machine Name",
    rtd.ReadingValue AS "Last Day Meter Reading"
from ElectricityDailyMeterReadingDetails as edmrd
    inner join ElectricityMachinePanelDetails as empd on empd.MachinePanelID = edmrd.MachinePanelID
    inner join ReadingTypesDetails as rtd on rtd.ReadingTypeID  = edmrd.ReadingTypeID
where
    rtd.ReadingCategoryID = 'RC001' and
    rtd.ReadingTypesDetails.ReadingTypeID = 'RT001'

如果你想加入 ReadingTypesDetails 两次,你可以很容易地做到这一点:

select
    empd.MachineDescription as "Machine Name",
    rtd.ReadingValue as "Last Day Meter Reading",
    rtd2.ReadingValue as "Another Reading"
from ElectricityDailyMeterReadingDetails as edmrd
    inner join ElectricityMachinePanelDetails as empd on empd.MachinePanelID = edmrd.MachinePanelID
    inner join ReadingTypesDetails as rtd on rtd.ReadingTypeID  = edmrd.ReadingTypeID
    inner join ReadingTypesDetails as rtd2 on rtd2.ReadingTypeID  = edmrd.ReadingTypeID
where
    rtd.ReadingCategoryID = 'RC001' and
    rtd.ReadingTypesDetails.ReadingTypeID = 'RT001' and
    --rtd2.ReadingCategoryID = '????' and
    --rtd2.ReadingTypesDetails.ReadingTypeID = '????' and
于 2013-10-28T12:24:32.477 回答
0

如果 datagridview 允许 doublename 我不确定,你可以只使用它两次。否则,只需给它另一个名称“Last Day Meter Reading Copy”。

Select 
ElectricityMachinePanelDetails.MachineDescription AS "Machine Name",
ReadingValue AS "Last Day Meter Reading",
ReadingValue AS "Last Day Meter Copy"
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' 

在 MSSQL 中,我认为你不能有两次相同的列名。但在 MySQL 中它很好。

于 2013-10-28T12:19:23.393 回答