gpt4 book ai didi

php - MYSQL 使用 AND 和 OR 从数据库中选择数据

转载 作者:行者123 更新时间:2023-11-29 20:13:32 25 4
gpt4 key购买 nike

我想从此表中选择 * 数据,以便根据我选择的过滤器显示所有内容。

我的 table :

tbl_预订

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 [昨天]
没有错误。
当我选择今天的过滤时..它会显示所有数据以及昨天的数据
但是当我选择昨天时..它会显示所有数据以及今天的数据

我希望的是纠正数据查询。有人可以帮忙吗?谢谢。

最佳答案

当您从日期中减去 1 时,它不会达到您想要的效果:MySql 然后将日期转换为数字并从中减去 1。这不会在本月的第一天给出预期的结果。

因此请使用 date_adddate_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)

关于php - MYSQL 使用 AND 和 OR 从数据库中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com