5

我有 2 个不同的查询将返回值:1502.00 和 6

SELECT replace(CURRENT_VALUE,'$','') curVal
FROM form_attributes_values
WHERE TEST_ID=2
AND ATTRIBUTE_ID = (
    SELECT ATTRIBUTE_ID
    FROM form_attributes
    WHERE FORM_ID=6
    AND FORM_FIELD_NAME='REGRINDABLECUTTERCOST'
)

SELECT replace(CURRENT_VALUE,'$','') curVal
FROM form_attributes_values
WHERE TEST_ID=2
AND ATTRIBUTE_ID = (
    SELECT ATTRIBUTE_ID
    FROM form_attributes
    WHERE FORM_ID=6
    AND FORM_FIELD_NAME='REGRINDSPOSSIBLE'
)

我正在尝试执行除法 query1/query2 (1502.00/6) 应该返回值250.333333

但我得到的价值是0.16666666666666

以下是尝试过的类型: 类型 1:

select (a.curVal / b.curVal) as final_count
from (
    SELECT replace(CURRENT_VALUE,'$','') curVal
    FROM form_attributes_values
    WHERE TEST_ID=2 AND ATTRIBUTE_ID = (
        SELECT ATTRIBUTE_ID
        FROM form_attributes
        WHERE FORM_ID=6
        AND FORM_FIELD_NAME='REGRINDABLECUTTERCOST'
    )
) a,
(
    SELECT replace(CURRENT_VALUE,'$','') curVal
    FROM form_attributes_values
    WHERE TEST_ID=2
    AND ATTRIBUTE_ID = (
        SELECT ATTRIBUTE_ID
        FROM form_attributes
        WHERE FORM_ID=6
        AND FORM_FIELD_NAME='REGRINDSPOSSIBLE'
    )
) b;

类型 2:

SELECT replace(CURRENT_VALUE,'$','') / (
    SELECT replace(CURRENT_VALUE,'$','') curVal
    FROM form_attributes_values
    WHERE TEST_ID=2
    AND ATTRIBUTE_ID = (
        SELECT ATTRIBUTE_ID
        FROM form_attributes
        WHERE FORM_ID=6
        AND FORM_FIELD_NAME='REGRINDSPOSSIBLE'
    )
) curVal
FROM form_attributes_values
WHERE TEST_ID=2
AND ATTRIBUTE_ID = (
    SELECT ATTRIBUTE_ID
    FROM form_attributes
    WHERE FORM_ID=6
    AND FORM_FIELD_NAME='REGRINDABLECUTTERCOST'
)

两种类型都返回0.166666666666667我错过了什么......?

4

4 回答 4

1

好的,所以我刚刚在 PHPmyAdmin 的 SQL 控制台中尝试了这个并得到了答案 250.3333

SELECT cast(1502.00 AS unsigned) / cast(6 AS unsigned);
于 2012-10-10T11:37:46.407 回答
1

看起来你只有第一个查询有问题,因为1 / 6 = 0.166666666666667

于 2012-10-10T11:33:09.187 回答
0

两种查询类型都可以正常工作,问题出在值中,存储在数据库中的值是字符串类型,并且值是逗号分隔的 (1,502.00) 而不是 (1502.00)

于 2012-10-10T11:49:34.207 回答
0
select sum(a.curVal) / sum(b.curVal) as final_count from 
(SELECT replace(CURRENT_VALUE,'$','') curVal FROM form_attributes_values WHERE TEST_ID=2     AND ATTRIBUTE_ID = (SELECT ATTRIBUTE_ID FROM form_attributes WHERE FORM_ID=6     AND FORM_FIELD_NAME='REGRINDABLECUTTERCOST')) a,
(SELECT replace(CURRENT_VALUE,'$','') curVal FROM form_attributes_values WHERE TEST_ID=2 AND ATTRIBUTE_ID = (SELECT ATTRIBUTE_ID FROM form_attributes WHERE FORM_ID=6 AND FORM_FIELD_NAME='REGRINDSPOSSIBLE')) b;

它未经测试,但使用 SUM 应该会有更好的运气。

希望这可以帮助

于 2012-10-10T11:20:33.427 回答