【问题标题】:Search data between date range AND add data between predefined date range在日期范围之间搜索数据并在预定义日期范围之间添加数据
【发布时间】:2015-07-18 07:15:30
【问题描述】:

我正在尝试构建一个搜索应用程序,人们可以根据输入的日期范围查看数据,但我也想添加预定义的日期范围并将此结果添加到最终输出中。

我一直在寻找解决方案,但我不知道该怎么做。我想我一定错过了什么。有人可以帮我分享一下知识吗?

仅供参考,我是编码新手。

这是我的表格:

    <table id="tbl_rekapbeli" class="easyui-datagrid" style="width:900px;height:360px"
        url="get_lap_beli2.php" pagination="true" idField="id"
        title="Rekap Pembelian Berkala" toolbar="#tb"
        singleSelect="true" fitColumns="true" showFooter="true">
    <thead>
            <th field="no_po" width="50" editor="{type:'validatebox',options:{required:true}}">No PO</th>
            <th field="tgl_po" width="50" editor="{type:'datebox',options:{required:true}}">Tgl PO</th>
            <th field="nama_outlet" width="50" editor="{type:'validatebox',options:{required:true}}">Customer</th>
            <th field="kode_barang" width="50" editor="{type:'validatebox',options:{required:true}}">Kode Barang</th>
            <th field="nama_barang" width="90" editor="{type:'validatebox',options:{required:true}}">Nama Barang</th>
            <th field="qty_beli" width="50" editor="{type:'validatebox',options:{required:true}}">Jumlah beli</th>
            <th field="harga_beli" width="50"  editor="{type:'validatebox',options:{required:true}}">Harga beli</th>
            <th field="jum_harga_beli" width="50" formatter="formatrp" editor="{type:'numberbox'}">Total</th>               
        </tr>
    </thead>
</table>

<div id="tb" style="padding:5px;height:auto">
    <div>
        Date From : <input id="date1" class="easyui-datebox" style="width:80px">&nbsp;&nbsp;
        To : <input id="date2" class="easyui-datebox" style="width:80px">&nbsp;&nbsp;
        <a href="#" id="aaa" class="easyui-linkbutton" iconCls="icon-search">Cari</a>
    </div>
</div>

函数脚本:

var d1 = 0
var d2 = 0

$('#date1').datebox({
    onSelect: function (date) {
        d1 = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
    }
})

$('#date2').datebox({
    onSelect: function (date) {
        d2 = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
    }
})

$('#aaa').click(function () {
    $('#tbl_rekapbeli').datagrid('options').url = "get_lap_beli2.php?start_date=" + d1 + "&end_date=" + d2;
    $('#tbl_rekapbeli').datagrid('reload');
    //alert(“dari: ” + d1 + ” sampai: ” + d2);
})

function formatrp(val, row) {
    return number_format(val, '', ',', '.');
};

function number_format(num, dig, dec, sep) {
    x = new Array();
    s = (num < 0 ? "-" : "");
    num = Math.abs(num).toFixed(dig).split(".");
    r = num[0].split("").reverse();
    for (var i = 1; i <= r.length; i++) {
        x.unshift(r[i - 1]);
        if (i % 3 == 0 && i != r.length) x.unshift(sep);
    }
    return "Rp " + s + x.join("") + (num[1] ? dec + num[1] : "");
}

对于行动:

$begin_date = '2015-07-01';
$start_date = $_REQUEST['start_date'];
$end_date = $_REQUEST['end_date'];

include '../../libs/conn.php';

$rs = mysql_query("SELECT count(*) FROM beli WHERE tgl_po between '$begin_date' and '$start_date' AND tgl_po between '$start_date' and '$end_date'");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];

$rs = mysql_query("SELECT * FROM beli WHERE tgl_po between '$begin_date' and '$start_date' AND tgl_po between '$start_date' and '$end_date'");

$items = array();
while($row = mysql_fetch_object($rs)){
array_push($items, $row);
}
$result["rows"] = $items;

$rs = mysql_query("SELECT SUM(jum_harga_beli) AS value_sum FROM beli WHERE tgl_po between '$begin_date' and '$start_date' AND tgl_po between '$start_date' and '$end_date'");
$rw = mysql_fetch_assoc($rs);
$sum = $rw['value_sum'];

$result["footer"]=array(array("harga_beli" => "Total Pembelian",'jum_harga_beli' => $sum));

echo json_encode($result);

【问题讨论】:

  • 运行时得到什么结果?任何错误输出?在任何人开始在他们自己的沙箱中运行此代码之前,发布运行代码的结果总是一种很好的礼仪。也可能存在配置问题。
  • 它仅显示所选日期范围内的数据,但我希望将预定义日期范围内的数据(从 2015-07-01 到开始日期)也添加到最终输出中

标签: javascript php mysql fifo stock


【解决方案1】:

您的查询有

  AND tgl_po between '$begin_date' and '$start_date'
  AND tgl_po between '$start_date' and '$end_date'

SQL 的工作方式,这里需要OR 而不是AND

  AND    (    tgl_po between '$begin_date' and '$start_date'
           OR tgl_po between '$start_date' and '$end_date')

如果您在 where 子句中提及 AND,它只接受匹配 both 条件的记录。如果您希望它符合任何一个条件,您需要OR

您希望行匹配第一个日期范围或第二个日期范围。所以你必须指定OR,而不是AND。在 SQL 查询中,AND 会缩小选择范围,OR 会扩大范围。

【讨论】:

  • 我确实需要两个日期范围的数据
猜你喜欢
  • 2019-11-05
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
相关资源
最近更新 更多