如果使用表

时间:2017-05-07 17:09:39

标签: php mysql sql

我的表格reservasi包含如下结构数据:

  • id_reservasi
  • package_name< - varchar
  • date_check_in< - varchar format(d-m-Y)
  • time_check_in< - varchar format(h:i:s)
  • price< - varchar

我想要选择数据,其中包括从之前数据reservasi到日期和时间之间的过滤器,直到指定的时间。

现在,我使用这个SQL查询:

SELECT SUM(price) AS total
FROM reservasi 
WHERE date(str_to_date(date_check_in,'%d-%m-%Y')) BETWEEN date(str_to_date('$date_check_in_rekap','%d-%m-%Y')) AND date(str_to_date('$date_now','%d-%m-%Y') 
AND STR_TO_DATE(time_check_in, '%H:%i:%s') BETWEEN STR_TO_DATE('$time_check_in_rekap', '%H:%i:%s') AND STR_TO_DATE('$time_now', '%H:%i:%s')

但它没有显示任何数据...... 如何正确使用BETWEEN

1 个答案:

答案 0 :(得分:2)

编辑,PDO版本:

$cek_pembelian=$this->pdo->prepare("
    SELECT 
        SUM(netto_bersih) AS netto_beli, 
        SUM(jumlah_dibayar) AS rupiah_beli 
    FROM p_penerimaan_tbs 
    WHERE str_to_date(CONCAT(tanggal,' ',jam_keluar) ,'%d-%m-%Y %H:%i:%s') 
        BETWEEN str_to_date(:datetime_start ,'%d-%m-%Y %H:%i:%s') AND now()
        ");
$cek_pembelian->execute(array(
    ':datetime_start'=>$tanggal_rekap .' '. $jam_rekap
    ));

这是你想要的吗?

SQL Fiddle

MySQL 5.6架构设置

查询1

SELECT SUM(price) AS total
FROM t 
WHERE str_to_date(CONCAT(date_check_in,' ',time_check_in) ,'%d-%m-%Y %H:%i:%s')
BETWEEN '2017-01-24 18:20:33' AND now()

<强> Results

| total |
|-------|
|   300 |