简体   繁体   中英

Mysql Get table result between two dates

I know my question is similar to other question already answered but my issue is different because I need some alternative or advice no how to go the other way around.

My issue is: I want to get values between either two dates or one according to what user wants..

在此处输入图片说明

When User request data of one day.. php query data successful.. but problem is when data requested is between two dates..

$query = $this->db->query("SELECT * FROM `meta_receipt_data` 
WHERE  `meta_transaction_date` >= '$first_date' AND
`meta_transaction_date` <= '$second_date' ");
        return $query->result();

I get an empty set...

So I thought may be the values are not submitted correct.. so I echoed the values to see if they are correct or not. I find they are correct...

$first_date = 09/13/2014;
$second_date = 09/19/2014;

But if I try to put the value like

$query = $this->db->query("SELECT * FROM `meta_receipt_data` 
WHERE  `meta_transaction_date` >= '09/13/2014' AND
`meta_transaction_date` <= '09/19/2014' ");
        return $query->result();

I get my result back correct.. so is there anything am doing it wrong??

MySQL有一个称为Between的内置函数,您可以像这样使用:

SELECT * FROM table_name WHERE date_column BETWEEN 'start_date_parameter' AND 'end_time_parameter'

Change the type of meta_transaction_date to DATE as that is what it is! Also use the standard 'yyyy-mm-dd' when passing in DATE s.

Your problem probably stems from string ordering of the 'mm/dd/yyyy' US date format which is horrible for coding. If you wish to display the DATE in this format, convert it when SELECT ing the final output.

Try to cast the date first , and then with between statement:

$query = $this->db->query("SELECT * FROM `meta_receipt_data` 
WHERE `meta_transaction_date` BETWEEN    
date_format(str_to_date('$first_date', '%d/%m/%Y'), '%Y-%m-%d') AND
date_format(str_to_date('$second_date', '%d/%m/%Y'), '%Y-%m-%d')");
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE  `meta_transaction_date` >= '09/13/2014'
AND `meta_transaction_date` <= '09/19/2014' ");

Since the above seems to be working fine, the problem is in your code.

$query = $this->db->query("SELECT `meta_transaction_date` FROM meta_receipt_data WHERE
                           meta_transaction_date BETWEEN "
                          .date( "Y-M-d", ( strtotime($first_date) ) )." AND "
                          .date( "Y-M-d", ( strtotime($second_date) ) ) );

A word of advice, do not use queries like SELECT * as they will degrade performance of your application. And I just read in your comment to your own question:

I have set the type as Varchar

Do not do that. It is best to use the DATE type to store dates. You can use the query:

ALTER TABLE `meta_receipt_data`
MODIFY `meta_transaction_date` DATE NOT NULL;`

Well, that is assuming you wish to keep the column to not accept null values.

I found that the value had space before and after so I use $first = trim($first_date); and problem solved...

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