0

我正在尝试构建一种计算器,它将在我的数据库中添加和相乘某些数据。

round()在嵌套选择(和其他计数)中正确使用 mysql 函数时遇到问题。

下面是我的代码示例,任何帮助将不胜感激。(代码看起来比实际上更复杂......)

如果我从每个嵌套选择中删除“选择回合”,这将有效。我的目标是对Floor这些计算中的每一个进行计算(某个数字 x 结果计数),但要对每个计算进行四舍五入。

select 
    FLOOR
    (
        select round(   1 * 
                (
                    SELECT count(action) from table
                )
        )
    +
        select round(   .5 *    
                (
                    SELECT count(action) from table

                )
        )
    -
        select round(   .5 *
                (
                    SELECT count(action) from table

                )
        )
    -
        select round(   .1 *
                (
                    SELECT count(action) from table

                )
        )
    ) as total
from table
LIMIT 0,1
4

2 回答 2

1

请试试

select 
    FLOOR
    (
        round(   1 * 
                (
                    SELECT count(action) from table
                )
        )
    +
        round(   .5 *    
                (
                    SELECT count(action) from table

                )
        )
    -
        round(   .5 *
                (
                    SELECT count(action) from table

                )
        )
    -
        round(   .1 *
                (
                    SELECT count(action) from table

                )
        )
    ) as total
from table
LIMIT 0,1

这是一个语法错误。

于 2012-10-17T01:30:45.883 回答
1

这就是你所追求的吗?因为所有的表名和字段名都是一样的,所以很难说你在做什么。但是,假设所有字段都来自一个表:

select 
  floor(
        round(   1 * count(action))
    +   round(  .5 * count(action))
    -   round(  .5 * count(action))
    -   round(  .1 * count(action))) 
from table
LIMIT 0,1
于 2012-10-17T01:49:48.097 回答