douli1872 2018-09-05 07:49
浏览 123
已采纳

MySQL查询使用日期范围获取数据

This query is giving correct result in MySql workbench -

SELECT tbl_visit_info.visitor_id,tbl_visit_info.retailer_id,tbl_retailer.retailer_shop_name,
  SUM( IF( tbl_visit_info.activity_type = 1 ,1,0)) AS feedback,
SUM( IF( tbl_visit_info.activity_type = 2 ,1,0)) AS promise_order,
SUM( IF( tbl_visit_info.activity_type = 3 ,1,0)) AS stock,
SUM( IF( tbl_visit_info.activity_type = 6 ,1,0)) AS payment,
tbl_visit_info.date as visit_date
FROM tbl_visit_info,tbl_retailer 
where tbl_visit_info.retailer_id=tbl_retailer.retailer_id 
and tbl_visit_info.visitor_id=80
group by visitor_id,retailer_id 
having date_format(tbl_visit_info.date, '%Y-%m-%d') BETWEEN '2018-07-01' AND '2018-09-02';

But, when I want to pass variables in place of 80 and date range as a variable then it is not giving any result. Query is-

SELECT tbl_visit_info.visitor_id,tbl_visit_info.retailer_id,tbl_retailer.retailer_shop_name,
  SUM( IF( tbl_visit_info.activity_type = 1 ,1,0)) AS feedback,
  SUM( IF( tbl_visit_info.activity_type = 2 ,1,0)) AS promise_order,
  SUM( IF( tbl_visit_info.activity_type = 3 ,1,0)) AS stock,
  SUM( IF( tbl_visit_info.activity_type = 6 ,1,0)) AS payment
  FROM tbl_visit_info,tbl_retailer
  where tbl_visit_info.retailer_id=tbl_retailer.retailer_id
  and tbl_visit_info.visitor_id='".$_GET["sr_id"]."'
  group by visitor_id,retailer_id having date_format(date, '%Y-%m-%d') BETWEEN 
  '".$_GET["start_date"]."' AND '".$_GET["end_date"]."'"

This query is not giving any result when I am writing code in PHP.

  • 写回答

1条回答 默认 最新

  • dpd66100 2018-09-05 07:51
    关注

    You should date filter in where clause instead of using having:

    SELECT tbl_visit_info.visitor_id,tbl_visit_info.retailer_id,tbl_retailer.retailer_shop_name,
      SUM( IF( tbl_visit_info.activity_type = 1 ,1,0)) AS feedback,
      SUM( IF( tbl_visit_info.activity_type = 2 ,1,0)) AS promise_order,
      SUM( IF( tbl_visit_info.activity_type = 3 ,1,0)) AS stock,
      SUM( IF( tbl_visit_info.activity_type = 6 ,1,0)) AS payment
      FROM tbl_visit_info,tbl_retailer
      where tbl_visit_info.retailer_id=tbl_retailer.retailer_id
      and tbl_visit_info.visitor_id='".$_GET["sr_id"]."' 
      and date_format(date, '%Y-%m-%d') BETWEEN 
      '".$_GET["start_date"]."' AND '".$_GET["end_date"]."'"
      group by visitor_id,retailer_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化