mysql : calculations on alias fields in mysql

标签 mysql

我已经提到过:

How to use alias as field in mysql

&

Adding MySQL alias fields together

我想对别名字段进行一些计算,但它会抛出错误

以下是我的查询

 select j.*,
    (select sum(stars) from ratingstar where jobid=j.id) as stars,
    (select count(*) from ratingstar where jobid=j.id) as count,
    ((stars/(count*5)*5)) as rating     //I have problem here if I remove this it works fine 
from jobs j inner join proposals p on p.jobid=j.id 
            inner join us_signup u on u.id=p.userid 
            inner join hired h on h.proposalid=p.id 
            where h.status="finished"

但它抛出错误

此选择行出错

select j.*,
    (select sum(stars) from ratingstar where jobid=j.id) as stars,
    (select count(*) from ratingstar where jobid=j.id) as count,
    ((stars/(count*5)*5)) as rating     //I have problem here if I remove this it works fine 

错误是

Fatal error: Uncaught exception 'Exception' with message 'Unknown column 'stars' in 'field list' query: select j.,(select sum(stars) from ratingstar where jobid=j.id) as stars,(select count() from ratingstar where jobid=j.id) as count,((stars/(count*5)*5)) as rating from jobs j inner join proposals p on p.jobid=j.id inner join us_signup u on u.id=p.userid inner join hired h on h.proposalid=p.id where h.status="finished"' in E:\wamp\www\sugumar\mysuite\includes\classes\MysqliDb.php on line 3637

最佳答案

我想你最后错过了一个)

((stars/(count*5)*5))

编辑*你没有错过,只是在开头加了一个没用的

(stars/(count*5)*5)

关于mysql : calculations on alias fields in mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44501094/

相关文章:

php - MySQL查询使用php基于列更新多行

PHP 执行一条插入 SQL 但在数据库中添加两条记录

mysql - Heroku rake 数据库 :structure:load with cleardb can't find 'mysql'

c++ - 无法从 C++ 向 MySQL 中插入数据

MySQL 从数据库中选择周范围

PHP 不会持续下去,如果

php - 回显数组值的问题

mysql - .Net 与 MySQL : ignore parameters in commandtext

java - 如何让 schemaspy 看到 mysql 连接器?

php - 使用 php 和 html 进行 for 循环