MySQL Round&子查询

时间:2014-01-14 09:42:56

标签: mysql

创建临时表时,我遇到圆函数问题。 (好吧,我认为无论如何这都是问题)

下面列表中的“EntryGauge”列需要根据数据库中的另一个值(不同的表)进行缩放和舍入。

我认为有2个查询可用作子查询来检索比例因子和舍入精度值。当我尝试使用它们时(如下面的查询中所示),会出现此错误:

  

“SQL错误(1166):列名不正确'ROUND(EntryGauge *(SELECT DisplayScaleFactor FROM webreportparametersetup WHERE MachineIdent ='Mac'”

我不确定问题是什么,或者我是否正确使用子查询但我真的可以使用一些帮助!

DROP TEMPORARY TABLE IF EXISTS LiveFeed;
CREATE TEMPORARY TABLE LiveFeed AS
(
SELECT 
    MachineIdent, 
    CoilID, 
    DATE_FORMAT(StartTime, '%b %d %Y %h:%i %p'), 
    DATE_FORMAT(EndTime, '%b %d %Y %h:%i %p'), 
    TIMEDIFF(EndTime, StartTime), 
    Alloy,
    ROUND(EntryGauge * (SELECT DisplayScaleFactor FROM webreportparametersetup WHERE MachineIdent = 'MachineIdent' AND ItemName = 'EntryGauge'), (SELECT DisplayPrecision FROM webreportparametersetup WHERE MachineIdent = 'MachineIdent' AND ItemName = 'EntryGauge')), 
    ExitGauge, 
    EntryWidth, 
    ExitWidth, 
    CoilDiameter, 
    CoilLength 
FROM Coil 
    INNER JOIN CoilPass 
    ON Coil.CoilIdent=CoilPass.CoilIdent 
    INNER JOIN PassSection
    ON CoilPass.PassIdent=PassSection.PassIdent 
WHERE PassSection.SectionIdent IN('7965','7966','7967')
);

修改:要清楚计算需要的内容:

  

Round(EntryGauge * ScaleFactor,RoundingPrecision)

2 个答案:

答案 0 :(得分:0)

请试试这个:

ROUND(EntryGauge * (SELECT DisplayScaleFactor 
FROM webreportparametersetup WHERE MachineIdent = 'MachineIdent' 
AND ItemName = 'EntryGauge'),2), ROUND((SELECT DisplayPrecision 
FROM webreportparametersetup WHERE MachineIdent = 'MachineIdent' 
AND ItemName = 'EntryGauge'),2)

我认为这是括号中的问题

答案 1 :(得分:0)

也许你只是忘了给圆柱一个名字? 加入为您提供了更好的概述:

DROP TEMPORARY TABLE IF EXISTS LiveFeed;
CREATE TEMPORARY TABLE LiveFeed AS
(
SELECT 
    MachineIdent, 
    CoilID, 
    DATE_FORMAT(StartTime, '%b %d %Y %h:%i %p'), 
    DATE_FORMAT(EndTime, '%b %d %Y %h:%i %p'), 
    TIMEDIFF(EndTime, StartTime), 
    Alloy,
    ROUND(EntryGauge * webreportparametersetup.DisplayScaleFactor,webreportparametersetup.DisplayPrecision) as RoundedValue, 
    ExitGauge, 
    EntryWidth, 
    ExitWidth, 
    CoilDiameter, 
    CoilLength 
FROM Coil 
    INNER JOIN CoilPass 
    ON Coil.CoilIdent=CoilPass.CoilIdent 
    INNER JOIN PassSection
    ON CoilPass.PassIdent=PassSection.PassIdent
    LEFT JOIN webreportparametersetup
    ON MachineIdent = 'MachineIdent' AND ItemName = 'EntryGauge'
WHERE PassSection.SectionIdent IN('7965','7966','7967')
);