duangua6912 2015-03-16 15:34
浏览 359
已采纳

SQL - 在WHERE条件中使用数学运算

I currently have this PHP/SQL code :

$today = date("Y-m-d");

$sqlQuery = 'SELECT id FROM post 
            WHERE (100*((agree+disagree)-('.nbDays('date', $today).')*10) >= 75)';

And my nbDays function.

function nbDays($debut, $fin) {
    $nbSecondes= 60*60*24;

    $debut_ts = strtotime($debut);
    $fin_ts = strtotime($fin);
    $diff = $fin_ts - $debut_ts;
    return round($diff / $nbSecondes);
}

Here is what my post table looks like :

Post
-- #id
-- [int] agree
-- [int] disagree
-- [datetime] date

What i want to do is return an array of ids for the post that match this condition;

(100 * ( agree/ (agree+disagree) )-(number of days from post date to today) ) > 75

I want to use an SQL request so that I don't have to list all posts before sorting them out.

I hope I'm clear enough, and wish you could help me.

Thanks a lot!

  • 写回答

1条回答 默认 最新

  • dongmeba4877 2015-03-16 16:18
    关注

    In your case you can write a database stored procedure to process your logic . then call database procedure directly in php . In short if you used MYSQL maybe you can try following simple query.

    SELECT id FROM post WHERE (100*((agree+disagree)-(UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(date))*10) >= 75);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?