简体   繁体   中英

Mysql between two datetime columns

I have the following code and it doesn't seem to be generating the expected results:

$date_from = '2012-04-27 18:19:33';
$date_to = '2012-05-29 00:59:57';

$database->connect(); 
        $ancestors = mysql_query('
            SELECT * FROM ' . $database->db_prefix . 'comments e
                WHERE e.created BETWEEN "' . $date_from . '" AND "' . $date_to . '" AND   
                    e.ancestors = "' . $comment["id"] . '" AND e.user_id != "' . $user->user_object["id"] . '" AND
                        NOT EXISTS
                        (
                            SELECT null 
                            FROM ' . $database->db_prefix . 'notifications d
                            WHERE  d.target_id = e.id
                        )
                        ORDER BY e.created DESC
        ', $database->connection_handle);
    $database->close();

It may be a silly error.. But I'm finding it hard to be sure when i'm unsure if the select statement BETWEEN and the dates being used are valid MYSQL things.

Any help would be great. I have been scouring and what I am doing seems to be correct thus the confusion.

EDITED //

I have many comments made between the date_from and date_to dates and there are no notifications in the notifications table (not exists part)... So basically, I should get about 25 comments back... Instead I just get one comment returned which has a date of 2012-05-29 00:23:39

EDITED //

Is it because you can only compare a date to a date.. and a time to a time? How might I adapt the mysql in order to do this?

EDITED //

It's definetley the between function going wrong. I'm really unsure of what the hell is going wrong with it.

  1. Can you return records between 2 dates?
  2. Can you give an example?

EDITED // Problem has been solved. It was nothing to do with the query so its safe to say this is the correct way of doing it. I had done my date ranges in a bonkers way. Thanks for all ur freaking help guys!!!!

In the MySQL Documentation it states you should use CAST when using between with date or time values, so you query should be written as follows.

WHERE e.created BETWEEN CAST("' . $date_from . '" AS DATETIME) AND CAST("' . $date_to . '" AS DATETIME) ...

This may fix your problem if you are not getting the results you expect with between.

I met the same problem and I think you may want to apply this solution:

$from = $_GET['from'];
$to = $_GET['to'];
include("connect.php");
$link=Connection();
$query = "SELECT * FROM records WHERE date_time BETWEEN '$from' AND '$to'";
$result=mysql_query($query,$link);
$list = array();
if($result === FALSE) {
    echo "No data";
}

The trick here is the signle quote, you need to put your variable inside that quote to let mysql handle it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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