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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料