【问题标题】:Having trouble using division in mysql [closed]在mysql中使用除法时遇到问题[关闭]
【发布时间】:2013-04-23 07:53:43
【问题描述】:

我想通过设计总浏览量/图片数量来获得人气: 这是代码:

$users = DB::query("SELECT * ,
  COUNT(p.id) as numPics,
  SUM(p.views) as totalViews
  COUNT(p.views)/SUM(p.id) as popularity
FROM
  images p 
INNER JOIN
  users u
ON
  p.user_id = u.id
 WHERE 
 p.created_at >= \"$current_month\"
GROUP BY p.user_id
ORDER BY popularity DESC LIMIT 10");

我收到了这个错误: 语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 'COUNT(p.views)/SUM(p.id) 附近使用的正确语法作为流行度

【问题讨论】:

  • totalViews 后面少了一个逗号

标签: php mysql laravel


【解决方案1】:

你在后面缺少一个逗号:

SUM(p.views) as totalViews

【讨论】:

    【解决方案2】:
    $users = DB::query("SELECT * ,
      COUNT(p.id) as numPics,
      SUM(p.views) as totalViews , 
      COUNT(p.views)/SUM(p.id) as popularity
    FROM
      images p 
    INNER JOIN
      users u
    ON
      p.user_id = u.id
     WHERE 
     p.created_at >= '$current_month'
    GROUP BY p.user_id
    ORDER BY popularity DESC LIMIT 10");
    

    【讨论】:

      猜你喜欢
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2013-02-25
      • 2014-08-13
      • 2015-01-24
      • 1970-01-01
      相关资源
      最近更新 更多