I wants to select rows which are less than with today date.
In database my date column is saving data in unix time stamp.
I am running on following query with php and mysql. (this query should show 1 row as i have 1 result in the database, but it is showing two rows.)
SELECT * from products where seller_id ='1' and FROM_UNIXTIME(marketing_end_date,'%Y-%m-%d') < CURRENT_DATE();
But when i run above query in phpmyadmin it retrun one row( my desired result).
Here is my code which is reading data from mysql.
function get_sellerenddateproducts($admin,$seller_id,$limit_per_page,$start_index){
$this->db->select('*');
$this->db->where(array('seller_id' => $seller_id));
$this->db->where("FROM_UNIXTIME(marketing_end_date,'%Y-%m-%d') <","CURRENT_DATE()");
$this->db->limit($limit_per_page, $start_index);
$this->db->order_by('id','desc');
$query = $this->db->from($this->_table_name)->get();
$arr = $query->result_array();
//echo $this->db->last_query(); die();
return $arr;
}
Question
Why same query show different results ? How to get correct results?
MySQL actually stores datetime data type fields internally as UTC.
However, PhpMyAdmin shows you the dates using the server default time.
Use this line to detect TimeZone of your MySQL Server:
SELECT ##system_time_zone;
For example, try adding this line before PhpMyAdmin SQL statement:
Set time_zone = '+00:00';
This sets timezone to GMT/UTC, so that any further operations will use GMT.
Let me know if they return same result now :)
I resolve my issue like below.
I create a variable like this.
$todaydate = date('Y-m-d');
and change my query from
$this->db->where("FROM_UNIXTIME(marketing_end_date,'%Y-%m-%d') <","CURRENT_DATE()");
to this
$this->db->where("FROM_UNIXTIME(marketing_end_date,'%Y-%m-%d') <",$todaydate);
then it gives me correct result.
Related
These expressions works perfectly:
$sql = "SELECT * FROM TABLE_SUITE_INSPECTION
WHERE QA_DATE = '$s_date'; returns the data for the requested date
$sql = "SELECT * FROM TABLE_SUITE_INSPECTION
WHERE QA_DATE = '$s_date' OR QA_DATE='$e_date'"; returns the data for the two required dates.
I can't figure out the reason for this statement not to work:
$sql = "SELECT * FROM TABLE_SUITE_INSPECTION
WHERE QA_DATE BETWEEN '$s_date' AND '$e_date'";
The SQL database has the QA_DATE as VARCHAR.
The data returned is always incomplete or with no results at all.
Example: The database has data for the following dates:
2/4/18
2/5/18
2/7/18
2/8/18
2/9/18
2/11/18
2/15/18
2/16/18
2/18/18
The results for s_date=2/4/18 and e_date=2/18/18 are none.
The result for s_date=2/4/18 and e_date=2/4/18 is correct.
The result for s_date=2/18/18 and e_date=2/18/18 is correct.
The results for s_date=2/4/18 and e_date=2/5/18 are correct.
The results for s_date=2/4/18 and e_date=2/7/18 are correct.
The results for s_date=2/4/18 and e_date=2/8/18 are correct.
The results for s_date=2/4/18 and e_date=2/9/18 are correct.
The results for s_date=2/4/18 and e_date=2/11/18 are none.
The result for s_date=2/11/18 and e_date=2/11/18 is correct.
The results for s_date=Any date and e_date=2/9/18 are correct.
Why is the query not picking up results for dates greater than 2/11/18?
Thanks for any help
Since the database column is a varchar, the data is being compared alphabetically. Alphabetically, 2/11/18 is the earliest value, so that's why you're seeing the results you're seeing. This is one reason why it's always best to use a date column type for dates.
You can probably make it work using https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date or similar. But if you can change the data set that would be a much better solution.
I have been trying to pull records based on a specified date. In the DB I have a column where I store unix timestamps. The user has the option to select records based on a date. The user inputs: 08/08/2016 (for example)
How would I write my SQL statement to handle this?
$query .= "`".$o."` = '".date("Y-m-d",strtotime($v))."' AND ";
Here we see the line where part of the SQL statement is built, because more than one column could be targeted by the user during their search request.
Somehow I need to be able to turn $o (which is a column storing unix timestamps) into the Y-m-d format for comparison, as shown above and make this work in an SQL statement.
Any help would be appreciated.
EDIT
Here is what worked for me:
$query .= "".$o.">= '".strtotime($v)."' AND".$o."< '".(strtotime($v)+(24*60*60))."' AND ";
You can do something like:
"WHERE DATE(`$o`) = '".date("Y-m-d",strtotime($v))."'"
However this won't use indexes, so if you have a large table it will be slower. If you want to be able to use indexes you can do:
"WHERE `$o` >= ".date("Y-m-d",strtotime($v))."
AND `$o` < ".date("Y-m-d",strtotime($v))." + INTERVAL 1 DAY"
You can also try this approach and use UNIX_TIMESTAMP on MySQL.
$v = '2016-08-04';
$query .= "'".$o."' = UNIX_TIMESTAMP('$v') AND ";
**How could I select the month part only in a DATE data-type column using CodeIgniter's active record class? What would be my query?
I'm also confused what do i need to use,.. LIKE or WHERE clause?
here is my model code snippet.**
`$previousMonth = date('m', strtotime(date('Y-m')." -1 month"));
$where = "MONTH(post_created_at),$previousMonth";
$this->db->select('*');
$this->db->from('tbl_post');
$this->db->where($where);
$this->db->where('post_approve', 'Yes');
$this->db->where('post_deleted', 'No');
$query=$this->db->get();
$countvalue = $query->num_rows();
echo "print_r($countvalue,TRUE);`
Well. WHERE is used if you want to select one row from the table. LIKE is used when you want more results than 2.
For example:
SELECT email FROM users WHERE email='$inputEmail';
SELECT name FROM users WHERE name='$firstName';
So, the first one you already know that it will return one value. The second returns various results.
You can think how a search engine works, it works with the statement LIKE for get a lot of result, and WHERE is used for logins.
I have my SQL statement like this trying to get the difference in 2 timestamps greater than 10 minutes. "timestamp" is a column in MYSQL which I hold a timstamp as such "1365793346"
SELECT * FROM table WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) AS thisisit
Im not sure if using "AS thisisit" is a current function of TIMESTAMPDIFF but I was able to find some old posts that how it used as such. I am not sure if its supported anymore because I an a syntax error at "AS thisisit"
I have also tried using
SELECT * FROM table WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) > 10
Where I am not sure what is going on is first is my syntax correct and second how to do associate this query with a label so I can echo it. My full PhP code looks like this
SELECT * FROM table WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) > 10
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row[0];
}
I was assuming I could use something like this to echo the results, but I get nothing to the screen. Can anyone point me in the right direction?
echo $row[0];
AS thisisit in this case have to be used to set an alias to your column.
So, you should use the following:
SELECT timestamp AS 'thisisit'
FROM table
WHERE TIMESTAMPDIFF(MINUTE, timestamp, NOW()) > 10;
So I am doing this query from PHP, and here listerally the exact query string:
SELECT * FROM `pdem_timesheet`.`tblMasterTimesheets` WHERE
`pdem_timesheet`.`tblMasterTimesheets`.`username` = 'pdem' AND
`pdem_timesheet`.`tblMasterTimesheets`.`date` >= '2012-05-09' AND
`pdem_timesheet`.`tblMasterTimesheets`.`date` <= '2012-05-15' ORDER BY
`pdem_timesheet`.`tblMasterTimesheets`.`date` ASC
It looks like it should be correct to me (more-or-less copying it from previous code I used that DOES work). But when I run the query, the results are empty.
If I change the query to not be a date range, but just a single day:
SELECT * FROM .... WHERE ...`date` = '2012-06-12' ....
it works just fine, returns the one result that it should.
I have tried using the between keyword:
SELECT * FROM ... WHERE ...`date` BETWEEN [start] [end]
but it still returns nothing...
Any ideas how to get this query to return a result?
===ANSWER===
When you go:
var curr_date = now.getDate();
var curr_month = now.getMonth();
var curr_year = now.getFullYear();
it returns the month - 1 for some reason. So if now's month is 6, now.getMonth() will return 5...Just need to add 1 in the query (wish I saw this sooner)
Your query seems to be working for me.
See demo.
Query I have is
SELECT * FROM tblMasterTimesheets
WHERE
username='pdem'
AND
date >= '2012-05-09' AND
date <= '2012-05-15'
ORDER BY date ASC
I assume, username is of type varchar and date is of type timestamp or datetime.
Similar to Fahim Parkar, here is an example of your query working with the use of the BETWEEN syntax: http://sqlfiddle.com/#!2/0fcb5/4
It sounds like your user pdem does not exist.
Make sure that:
There is in fact a result which should be returned when using your
given criteria. Make sure the DD-MM-YYYY syntax is correct and make sure you know which month is which number (may is 05, june is 06)
That the datatype of the column date is of a date
type, not a generic text/varchar type. You cannot compare varchar with >= like that (not the way you want, at least. Only works on date types)
As Fahim said, your code is correct. It must be something within the table which is causing your issues.