我想从此表中选择*数据,只是根据我选择的过滤器显示所有内容。
我的表:
tbl_reservation
res_id | trans_code | status | order_type | type_of_order | date_ordered
------ | -----------|----------|--------------|-------------------|-------------
1 | 111-111 |Delivered | For Delivery | Online Transaction|2016-10-09
------ | -----------|----------|--------------|-------------------|-------------
2 | 111-112 |Delivered | For Pick-up | Online Transaction|2016-10-09
------ | -----------|----------|--------------|-------------------|-------------
3 | 111-113 |Cancelled | For Delivery | Online Transaction|2016-10-09
------ | -----------|----------|--------------|-------------------|-------------
4 | 111-114 | | For Purchase | Online Transaction|2016-10-09
------ | -----------|----------|--------------|-------------------|-------------
5 | 111-115 | | For Pick-up | Walkin Transaction|2016-10-09
------ | -----------|----------|--------------|-------------------|-------------
6 | 111-116 | | For Purchase | Walkin Transaction|2016-10-09
------ | -----------|----------|--------------|-------------------|-------------
7 | 111-117 |Delivered | For Pick-up | Walin Transaction |2016-10-09
------ | -----------|----------|--------------|-------------------|-------------
8 | 111-118 |Delivered | For Delivery | Online Transaction|2016-10-08
------ | -----------|----------|--------------|-------------------|-------------
9 | 111-119 |Delivered | For Pick-up | Online Transaction|2016-10-08
------ | -----------|----------|--------------|-------------------|-------------
10 | 111-110 |Cancelled | For Delivery | Online Transaction|2016-10-08
------ | -----------|----------|--------------|-------------------|-------------
11 | 111-100 | | For Purchase | Online Transaction|2016-10-08
------ | -----------|----------|--------------|-------------------|-------------
12 | 111-101 | | For Pick-up | Walkin Transaction|2016-10-08
------ | -----------|----------|--------------|-------------------|-------------
13 | 111-102 | | For Purchase | Walkin Transaction|2016-10-08
------ | -----------|----------|--------------|-------------------|-------------
14 | 111-103 |Delivered | For Pick-up | Walin Transaction |2016-10-08
我的表格:
<form method="post" action="">
<select name="filter">
<option value="Today">Filter Data for today</option>
<option value="Yesterday">Filter Data for yesterday</option>
</select>
<input type="submit" value="Filter"/>
</form>
我的PHP代码与我的样本一起获取过滤器
<?php
//inclue db connection
if(isset($_POST["filter"])){
$get_filter = $_POST["filter"];
if($filter == "Today"){
$sql = mysql_query("SELECT * FROM tbl_reservation WHERE status = 'Delivered' OR status = 'Cancelled' OR status = '' AND DATE(date_ordered) = DATE(now()) ");
}elseif($filter == "Yesterday"){
$sql = mysql_query("SELECT * FROM tbl_reservation WHERE status = 'Delivered' OR status = 'Cancelled' OR status = '' AND DATE(date_ordered) = DATE(now())-1 ");
}
//THEN DISPLAY ALL IN VARIABLE USING WHILE LOOP AS mysql_fetch_array
//Display data here
//..
//..
//..
?>
日期订单= 2016-10-09 [今天]
日期顺序= 2016-10-08 [昨天]
没有错误。
当我今天选择过滤时,它会显示所有数据以及昨天的数据
但是当我昨天选择时......它会显示所有数据以及今天的数据
我希望更正数据查询。有人可以帮忙吗?谢谢。
答案 0 :(得分:0)
这会有用吗?
SELECT * FROM tbl_reservation WHERE DATE(date_ordered) = DATE(now()) AND (status = 'Delivered' OR status = 'Cancelled' OR status = '')
答案 1 :(得分:0)
你应该使用date_sub
SELECT *
FROM tbl_reservation
WHERE status = 'Delivered'
OR status = 'Cancelled'
OR status = ''
AND DATE(date_ordered) = DATE_SUB(now(),INTERVAL 1 DAY)
也可能是状态为空(不是='')
SELECT *
FROM tbl_reservation
WHERE status = 'Delivered'
OR status = 'Cancelled'
OR status is null
AND DATE(date_ordered) = DATE_SUB(now(),INTERVAL 1 DAY)
答案 2 :(得分:0)
当你从日期中减去1时,它没有你想要的效果:MySql然后将日期转换为数字并从中减去1。这不会在本月的第一天给出预期的结果。
因此,请使用date_add
或date_sub
代替-1
,例如:
date_sub(date(now()), interval 1 day)
其次,逻辑运算符and
优先于or
,所以当你写这个时:
WHERE status = 'Delivered'
OR status = 'Cancelled'
OR status = ''
AND date(date_ordered) = date(now())
评估为:
WHERE status = 'Delivered'
OR status = 'Cancelled'
OR (status = '' AND date(date_ordered) = date(now()))
...这意味着当该状态为已交付或已取消时,日期条件不起任何作用。
您可以通过在OR
部分周围添加括号来解决此问题,或者您可以从IN
运算符中受益:
WHERE status IN ('Delivered', 'Cancelled', '')
AND date(date_ordered) = date(now()))
..和第二个SQL语句:
WHERE status IN ('Delivered', 'Cancelled', '')
AND date(date_ordered) = date_sub(date(now()), interval 1 day)