如何在mysql中使用特定日期范围制作报告 [英] How Make reports with specific range of dates in mysql

查看:87
本文介绍了如何在mysql中使用特定日期范围制作报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在使用商店收入的每日报告,但是我需要执行以下报告:我想查看特定日期范围(或几周或几个月)的收入报告.我可以选择要查看的日期或日期范围,以便打印我的选择.

I am already using a daily report of the income of the store, but I need my report to do the following: i want to see the income report of specific range of dates, or weeks, or months.... essentially that i can choose which days or range of days i want see so i can print my selection.

这是inc.php

<?
$dbtype     = "mysql";
$dbhost     = "localhost";
$dbname     = "anillos";
$dbuser     = "rubi";
$dbpass     = "----";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$conn->exec("set names utf8");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
?>

这是我的代码:

<table class="table-bordered table-striped table-condensed">
    <thead>
        <tr>
          <th>Fecha</th>
          <th>Total Anillos Vendidos</th>
          <th>Total ganado del día</th>                                 
        </tr>
    </thead>   
    <tbody id="result_fechas">
        <tr>
            <? 
                include 'inc.php';
                $sql = $conn->prepare("SELECT DATE(start) AS date, COUNT(id_anillos) AS total_anillos, SUM(ventas) AS total_diario_ganado
                FROM PRODUCTOS WHERE start >= CURDATE() AND start < CURDATE() + INTERVAL 1 DAY ORDER BY start ASC");
                $sql->execute();
                while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
            ?>
            <td class="center"><? echo $row['date']; ?></td>
            <td class="center"><? echo $row['total_anillos']; ?></td>
            <td class="center"><? echo $row['total_diario_ganado']; ?></td>                 
        </tr>   <? } ?>                          
    </tbody>

你能帮我吗..我真的没有头绪...

can you help me with this..I really not have a clue...

----更新-----

----UPDATE-----

@ Chris78,我填写了以下表格:

@ Chris78 I made this form:

<form name="fechas" id="fechas" method="post" >
<fieldset>
<legend>Reporte desde : </legend>
<input type="text" value="" placeholder="YYYY-MM-DD" name="f_desde" id="datepicker"/>
<legend>Reporte hasta : </legend>
<input type="text" value="" placeholder="YYYY-MM-DD" name="f_hasta" id="datepicker2" />
<button class="btn btn-inverse" type="submit" name="enviar" >
<i class="icon icon-print icon-white"></i> 
Ver Reporte                                     </button>
</fieldset>
</form>

新的选择代码:

<? 
$sql = $conn->prepare("SELECT DATE(start) AS date, COUNT(id_anillos) AS total_anillos, SUM(ventas) AS total_ganado FROM PRODUCTOS WHERE start BETWEEN 'f_desde' AND 'f_hasta' ORDER BY start ASC");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
?>

表格:

<table class="table-bordered table-striped table-condensed">
    <thead>
        <tr>
          <th>rango de fechas seleccionado</th>
          <th>Total Anillos Vendidos</th>
          <th>Total ganado</th>                                 
        </tr>
    </thead>   
    <tbody id="result_fechas">
        <tr>
            <? 
           ////////SELECT CODE HERE
            ?>
            <td class="center"><? echo $row['date']; ?></td>
            <td class="center"><? echo $row['total_anillos']; ?></td>
            <td class="center"><? echo $row['total_ganado']; ?></td>                 
        </tr>   <? } ?>                          
    </tbody>

并且我尝试在同一页面中通过ajax调用显示结果

and I am try to show the result with ajax call in the same page

<script type="text/javascript">
      $(function(){
        $("#fechas").submit(function(){
          $.ajax({
            type:"POST",
            url:".reportes.php?ts=" + new Date().getTime(),
            dataType:"text",
            data:$(this).serialize(),
            beforeSend:function(){
              $("#loading").show();
            },
            success:function(response){
                $("#result_fechas").append(response);
                $("#loading").hide();
            } 
          })
          return false;
        });
    });
    });
    </script>

reportes.php是同一页,表单和正在等待结果的表在哪里....但我不知道错误在哪里,因为没有捕获ajax数据,并且当我刷新页面时按一下按钮...您能帮我这个忙吗?

reportes.php is the same page where is the form and the table which is waiting the result....but I don´t know where is the error, because not catch ajax the data and the page is refreshing when I clic the button...can you help me with this.

推荐答案

如果要使用日期范围运行query,则可以使用BETWEEN语句.

If you are wanting to run a query with a date range you can use the BETWEEN statement.

$sql = $conn->prepare("SELECT DATE(start) AS date, COUNT(id_anillos) AS total_anillos, SUM(ventas) AS total_diario_ganado
                FROM PRODUCTOS WHERE start BETWEEN 'Older Date Here' AND 'Newer Date Here' ORDER BY start ASC");

这篇关于如何在mysql中使用特定日期范围制作报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆