简体   繁体   English

SQL根据日期提取数据

[英]SQL pulling data based on date

I have a database full of store information and personal transactions. 我有一个充满商店信息和个人交易的数据库。 I have two webpages. 我有两个网页。 One takes user input for the store name and runs a query pulling all transaction info for that store. 一个将用户输入的商店名称作为输入,然后运行查询以获取该商店的所有交易信息。 I have another page almost identical that takes input for a start date and an end date and then runs a query for transactions between that date. 我还有另一个页面几乎完全相同,该页面需要输入开始日期和结束日期,然后运行该日期之间的交易查询。 The one for the store name works perfectly, but the one for the date doesn't display anything. 商店名称的一个很好用,但是日期的一个不显示任何内容。 I am still learning html/php/sql but I know exactly how the first page works so I cannot see why the same doesn't work for the second. 我仍在学习html / php / sql,但是我确切知道第一页的工作原理,所以我看不到为什么第二页不起作用。 If someone could help me, I'd appreciate it. 如果有人可以帮助我,我将不胜感激。

Here is my first page for the store name search that works. 这是我搜索商店名称的第一页。

<?php

$transaction = $_REQUEST["StoreName"];

    require_once 'login.php';

    $connection = mysqli_connect(
           $db_hostname, $db_username,
           $db_password, $db_database);



       $sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
        $result = $connection->query($sql);

    ?>

    Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
    <tr>
                  <th width="15%">Item Name</th>
                  <th width="15%">Item Price</th> 
                  <th width="15%">Purchase Time</th>
                  <th width="15%">Purchase Date</th>
                  <th width="15%">Category</th>
                  <th width="15%">Rating</th>
                </tr>
</table>
     <?php
        if($result->num_rows > 0){
            // output data of each row
            while($rows = $result->fetch_assoc()){ ?>
               <table border="2" style="width:100%">
                <tr>
                    <td width="15%"><?php echo $rows['ItemName']; ?></td>
                    <td width="15%"><?php echo $rows['ItemPrice']; ?></td>
                    <td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
                    <td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
                    <td width="15%"><?php echo      $rows['PurchaseCategory']; ?></td>
                    <td width="15%"><?php echo $rows['Rating']; ?></td>
                </tr>
                <?php
            }
        }
        ?>

And here is the page for the date search that returns no data. 这是日期搜索页面,不返回任何数据。

<?php

$startDate = $_REQUEST["StartDate"];
$endDate = $_REQUEST["EndDate"];

    require_once 'login.php';

    $connection = mysqli_connect(
           $db_hostname, $db_username,
           $db_password, $db_database);



       $sql = "SELECT * FROM PURCHASE WHERE PurchaseDate BETWEEN '%".$startDate."%' and '%".$endDate."%'";
        $result = $connection->query($sql);

    ?>

    Purchases Made Between <?php echo $startDate ?> and <?php echo $endDate ?>
<table border="2" style="width:100%">
    <tr>
                  <th width="15%">Item Name</th>
                  <th width="15%">Item Price</th> 
                  <th width="15%">Purchase Time</th>
                  <th width="15%">Purchase Date</th>
                  <th width="15%">Category</th>
                  <th width="15%">Rating</th>
                </tr>
</table>
     <?php
        if($result->num_rows > 0){
            // output data of each row
            while($rows = $result->fetch_assoc()){ ?>
               <table border="2" style="width:100%">
                <tr>
                    <td width="15%"><?php echo $rows['ItemName']; ?></td>
                    <td width="15%"><?php echo $rows['ItemPrice']; ?></td>
                    <td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
                    <td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
                    <td width="15%"><?php echo $rows['PurchaseCategory']; ?></td>
                    <td width="15%"><?php echo $rows['Rating']; ?></td>
                </tr>
                <?php
            }
        }
        ?>

Obviously the initial search pages that link to both of these output pages differ a bit more but I can confirm that the date output page is receiving the correct dates from the search page. 显然,链接到这两个输出页面的初始搜索页面稍有不同,但是我可以确认日期输出页面正在从搜索页面接收正确的日期。

EDIT: Here is the search page for the date. 编辑:这是日期的搜索页面。

<!DOCTYPE html>
<html>
<head>
<title>Output 2</title>
</head>
<body>
<h1>Required Output 2</h1>
<h2>Transaction Search By Date</h2>
<br/>
<br/>

<form action="outputout2.php" method="get">
Start Date: <input type="date" name="StartDate">
End Date: <input type="date" name="EndDate">
<input name="Add Merchant" type="submit" value="Search">
</form>;

</body>
</html>

This sort of problem is usually the result of misformatted dates. 这类问题通常是日期格式错误的结果。

Your query, if I'm reading it right, says this when you've finished assembling it in php and sent it to the SQL server: 如果我没看错,您的查询将在php中完成组装并将其发送到SQL Server时说:

 SELECT *
   FROM PURCHASE                   --wrong!
  WHERE PurchaseDate BETWEEN '%5/1/2016%' and '%5/8/2016%'

This is not going to work. 这是行不通的。

For one thing, the % signs only work with the LIKE operator, not on equality and inequality operators. 一方面, %符号仅适用于LIKE运算符,不适用于相等和不相等运算符。

For another thing, different makes and models of database server software use differently formatted date strings. 另一方面,不同品牌和型号的数据库服务器软件使用不同格式的日期字符串。 MySQL and SQL Server will work fine with string formats like 2016-05-01 . MySQL和SQL Server可以很好地与2016-05-01这样的字符串格式配合使用。 But you have to look up the appropriate ways of doing this for the kind of SQL software product you are using. 但是,您必须针对所使用的SQL软件产品查找执行此操作的适当方法。 This kind of stuff is not very vendor-portable. 这种东西不是非常适合厂商移植。

Third: your PurchaseDate column in your table needs to have a date-like datatype for this kind of thing to work. 第三:表中的PurchaseDate列需要具有类似日期的数据类型,这样的事情才能起作用。

Fourth: if your PurchaseDate items are timestamps -- that is, if that have both dates and times -- you can't reliably use the BETWEEN operator. 第四:如果您的PurchaseDate项目是时间戳记(即同时具有日期和时间),则无法可靠地使用BETWEEN运算符。 Here's why: Suppose you have a value of 2016-05-08 11:50:00 When you compare that to an end date 2016-05-08 , it lies beyond the end date. 原因如下:假设您的值是2016-05-08 11:50:00将其与结束日期2016-05-08进行比较,则该值超出了结束日期。 So, what you really need is this: 因此,您真正需要的是:

 WHERE PurchaseDate >= '2016-05-01'
   AND PurchaseDate <  '2016-05-09' -- the day AFTER your end date

That construct performs the same as BETWEEN and doesn't screw up the last days' data. 该构造与BETWEEN表现相同,并且不会BETWEEN过去几天的数据。

Edit so it's MySQL you're using. 编辑,以便使用MySQL。

Workbench is a client program. Workbench是一个客户端程序。 So is PHP. PHP也是如此。 The server is MySQL. 服务器是MySQL。 When you do an operation like 当您执行类似的操作时

     DateColumn < 'string constant'

MySQL implicitly converts the string constant to a date. MySQL将string constant隐式转换为日期。 If the format of the string constant is close enough to ISO 8601 for the server to figure out what it means, it will convert it. 如果字符串常量的格式与ISO 8601足够接近,以使服务器知道它的含义,它将对其进行转换。 2016-12-13 is exactly right. 2016-12-13完全正确。 2016-5-31 is close enough for MySQL, and so is 2016-5-5 . 2016-5-31是MySQL足够接近,所以是2016-5-5 2016/12/31 doesn't work. 2016/12/31不起作用。 Neither does 5/5/16 . 5/5/16

Germane to your question is this: MySQL cannot convert %2016-5-5% (with the percent signs) to a date. 与您的问题相关的问题是:MySQL无法将%2016-5-5% (带有百分号)转换为日期。

When MySQL can't convert a date, it comes up with NULL. 当MySQL无法转换日期时,它会带有NULL。 Comparisons with NULL values are weird . 与NULL值的比较很奇怪 And, in the immortal words of Hunter S. Thompson, "when the going gets weird, the weird turn pro" . 而且,用亨特·汤普森(Hunter S. Thompson)不朽的话说, “当事情变得怪异时,怪异的转身职业” NULL weirdness is probably the root cause of your empty result set in the query you showed. 空怪异可能是您显示的查询中空结果集的根本原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM