在Mysql查询中划分

时间:2012-10-10 11:09:50

标签: mysql division

我有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'
)

我正在尝试执行division 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 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

SELECT cast(1502.00 AS unsigned) / cast(6 AS unsigned);

答案 2 :(得分: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应该会有更好的运气。

希望这有帮助

答案 3 :(得分:0)

两种查询类型都能正常工作,问题出在值中,存储在Database中的值是String类型,值也是逗号分隔(1,502.00)而不是(1502.00)