gpt4 book ai didi

php - 当我向其添加日期范围时,我的查询将不起作用。使用 PHP 和 MySQL

转载 作者:行者123 更新时间:2023-11-29 06:57:31 24 4
gpt4 key购买 nike

我目前在一个表单中有多个搜索字段,用户可以选择要过滤的字段,一切正常,直到我向其中添加日期范围。如果我单独添加日期范围而没有任何其他字段来过滤它,那么我知道这并不是我写错了查询。请帮我解决这个问题。谢谢!

这是有效的查询:(同样出于某种原因,执行 SELECT * 将不起作用,我必须键入要显示的所有字段的名称)

它包括表单中包含日期范围的部分。

<li class="form-line form-line-column" id="id_29">
<label class="form-label-top" id="label_29" for="input_30">
From Date:
</label>
<div id="cid_29" class="form-input-wide">
<input type="text" class="form-textbox validate" id="datepicker" name="from_date" size="10" />
</div>
</li>
<li class="form-line form-line-column" id="id_29">
<label class="form-label-top" id="label_29" for="input_8">
To Date:
</label>
<div id="cid_29" class="form-input-wide">
<input type="text" class="form-textbox validate" id="datepicker2" name="to_date" size="10" />
</div>
</li>

<?php
$sso = $_GET['sso'];
$assigned_to = $_GET['assigned_to'];
$case_status = $_GET['case_status'];
$startdate = $_GET['from_date'];
$enddate = $_GET['to_date'];
$subject = $_GET['subject'];

$sql = ("SELECT
id, sso, full_name, case_status, assigned_to, resolution,
description, updated, created, additional_notes, subject
FROM rmstable2
WHERE
sso LIKE '%$sso%' AND
case_status LIKE '%$case_status%' AND
assigned_to LIKE '%$assigned_to%' AND
subject LIKE '%$subject%'");
?>

这是我想要的查询:

<?php
$sql = ("SELECT
id, sso, full_name, case_status, assigned_to, resolution,
description, updated, created, additional_notes, subject
FROM rmstable2
WHERE
sso LIKE '%$sso%' AND
case_status LIKE '%$case_status%' AND
assigned_to LIKE '%$assigned_to%' AND
subject LIKE '%$subject%'
AND created >= '$startdate'
AND created <= '$enddate'");
?>

最佳答案

为此使用 BETWEEN 并且不要忘记用 DATE( ) 包裹日期

$sql = ("SELECT id, sso, full_name, case_status, assigned_to, 
resolution, description, updated, created,
additional_notes, subject
FROM rmstable2
WHERE sso LIKE '%$sso%' AND
case_status LIKE '%$case_status%' AND
assigned_to LIKE '%$assigned_to%' AND
subject LIKE '%$subject%' AND
DATE(created) BETWEEN DATE('$startdate') AND DATE('$enddate')");

更新 1

如何将 AND 更改为 OR

$sql = ("SELECT id, sso, full_name, case_status, assigned_to, 
resolution, description, updated, created,
additional_notes, subject
FROM rmstable2
WHERE sso LIKE '%$sso%' OR
case_status LIKE '%$case_status%' OR
assigned_to LIKE '%$assigned_to%' OR
subject LIKE '%$subject%' OR
(DATE(created) BETWEEN DATE('$startdate') AND DATE('$enddate'))");

关于php - 当我向其添加日期范围时,我的查询将不起作用。使用 PHP 和 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11866863/

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