Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysql中的WHERE问题_Php_Mysql_Laravel - Fatal编程技术网

Php mysql中的WHERE问题

Php mysql中的WHERE问题,php,mysql,laravel,Php,Mysql,Laravel,嗨,伙计们,我正在使用这个MYSQL查询来获取当前月份最热门的上传用户和他们的图像浏览总数: $users = DB::query("SELECT * , COUNT(p.id) as numPics, SUM(p.views) as totalViews FROM images p INNER JOIN users u ON p.user_id = u.id WHERE p.created_at >= \"$current_month\" GROUP

嗨,伙计们,我正在使用这个MYSQL查询来获取当前月份最热门的上传用户和他们的图像浏览总数:

    $users = DB::query("SELECT * ,
  COUNT(p.id) as numPics,
  SUM(p.views) as totalViews

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 totalViews DESC LIMIT 10");
totalViews返回所有图片的总视图的麻烦,我想要的是获得当月上传的图片的总视图。 谢谢。

使用mysql中的MONTHNOW获取当月数据

p.created_at >= MONTH(NOW())

将GROUPBY语句和select子句更改为

SELECT * ,
  (SELECT COUNT(*) FROM images where id = p.id) as numPics,
  SUM(p.views) as totalViews

FROM
  images p 
INNER JOIN
  users u
ON
  p.user_id = u.id
 WHERE 
 p.created_at >= \"$current_month\"
GROUP BY p.user_id, p.id
ORDER BY totalViews DESC LIMIT 10"
你需要使用


变量的名称是miss leading,即$current_month,但您的评论说它有一个时间戳值,例如2013-05-01 23:59:59

此查询看起来是一个完美的示例,可以使它更通用一些。在为创建的月份添加上限时,还可以使用相同的查询查找上个月或任何其他月份的顶部

$lower_limit = date('Y-m-d', strtotime('first day of this month'));
$upper_limit = date('Y-m-d', strtotime('first day of next month'));
我假设您使用的是PDO,所以我只提供SQL

SELECT u.*,
  COUNT(p.id) as numPics,
  SUM(p.views) as totalViews
FROM
  images p 
INNER JOIN
  users u
ON
  p.user_id = u.id
WHERE 
  p.created_at >= :lower_limit
AND p.created_at < :upper_limit
GROUP BY p.user_id
ORDER BY totalViews DESC
LIMIT 10

请注意,这不会选择任何图像。结果集应该是一致的,当您按用户id分组时,您无法控制结果集中的图像数据。

虽然这是一个很好的建议,但它不会直接回答问题。考虑扩展你的答案来确定OP当前的问题。我使用这个结构:$CurrnEngays= DAT'Y-M-D',这个月的STRTO时间第一天。23:59:59';创建的列的数据类型是什么?@Hamza:mysql本身就有获取当月的func。那么你为什么要用php来获取当前月份并在查询中实现呢?嗯,是不是很简单:更改strto*time*in strto*date*?你能直接把代码放在哪里我能找到解决方案吗?bcz除了我发布的代码外,完整的代码工作得很好。你尝试过子查询了吗?请尝试一下你可能得到的结果可以放一些exmaple plz吗?选择*,COUNTp.id作为numPics,选择SUMp.views作为totalViews,其中user_id=p.id尝试类似以下语法错误或访问冲突:1064您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以了解在“WHERE p.created_at>=2013-05-01 23:59:59”附近使用的正确语法
SELECT u.*,
  COUNT(p.id) as numPics,
  SUM(p.views) as totalViews
FROM
  images p 
INNER JOIN
  users u
ON
  p.user_id = u.id
WHERE 
  p.created_at >= :lower_limit
AND p.created_at < :upper_limit
GROUP BY p.user_id
ORDER BY totalViews DESC
LIMIT 10