I want to update mysql rows where DATETIME < TIMESTAMP
DATETIME is like: "2014-06-21 17:56:00"
TIMESTAMP is like 1454546656 (which is now)
I want to update all the rows where DATETIME is in the past
What's the lightest method to deal with a huge number for rows?
Thanks.
In mysql there 2 function which are
from_unixtime() to convert the unix time to human readable date
unix_timestamp() to convert a human readable date to timestamp
So you can use one of then for the comparison
Here how it looks
mysql> select from_unixtime(1454546656);
+---------------------------+
| from_unixtime(1454546656) |
+---------------------------+
| 2016-02-04 06:14:16 |
+---------------------------+
mysql> select unix_timestamp('2014-06-21 17:56:00');
+---------------------------------------+
| unix_timestamp('2014-06-21 17:56:00') |
+---------------------------------------+
| 1403353560 |
+---------------------------------------+
mysql> select unix_timestamp('2014-06-21 17:56:00') < 1454546656;
+----------------------------------------------------+
| unix_timestamp('2014-06-21 17:56:00') < 1454546656 |
+----------------------------------------------------+
| 1 |
+----------------------------------------------------+
mysql> select from_unixtime(1454546656) > '2014-06-21 17:56:00';
+---------------------------------------------------+
| from_unixtime(1454546656) > '2014-06-21 17:56:00' |
+---------------------------------------------------+
| 1 |
+---------------------------------------------------+
So its upto you which one you want to use for the comparison.
Since you are using PHP, try using the PHP Date Function:
$ts = date("Y-m-d H:i:s", $timestamp); //Convert Unix Timestamp to MySQL Date/Time Format
UPDATE table WHERE DATETIME < '$ts';
This is the lightest method I can think of, because it is not recalculating the timestamp for each record, and furthermore, if the DateTime Field is indexed, it will go incredibly fast.
Related
Here is my table num structure:
mysql> show columns from num;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| ip | char(20) | YES | | NULL | |
| time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.00 sec)
I insert record with the following codes:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$time=$_SERVER['REQUEST_TIME'];
$db=mysql_connect("localhost","root","passwd");
$query="insert into num(ip,time) values('$ip','$time')";
mysql_select_db('numdb');
mysql_query($query, $db);
mysql_close();
echo "ok";
?>
The time is wrong after i inserted two records into table num,
What is matter with my database or php code?
mysql> select * from num;
+-----------+---------------------+
| ip | time |
+-----------+---------------------+
| 127.0.0.1 | 0000-00-00 00:00:00 |
| 127.0.0.1 | 0000-00-00 00:00:00 |
+-----------+---------------------+
2 rows in set (0.00 sec)
To insert the current timestamp, use:
$query="INSERT INTO num(time) VALUES (CURRENT_TIMESTAMP)";
If your server time is different from what you want, you can add hours (or minutes, etc) to the timestamp:
$query="INSERT INTO num(time) VALUES (CURRENT_TIMESTAMP + INTERVAL 6 HOUR)";
It looks like you want to use the REQUEST_TIME value instead of the current time. Fortunately, php makes it easy to convert the string format of the request time into an internal time format, then convert that to the
2015-03-14 15:19:27
standardized string format needed by MySQL to represent a timestamp. This line will give you the workable datestring.
$datestring = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
If you don't care about the distinction between the REQUEST_TIME and the current time, simply don't mention the timestamp column in your INSERT query, and MySQL will use the default (the present time).
Pro tip: Don't name a column timestamp because that's a reserved word. You have to wrap it in backticks in some contexts. Many developers use ts for this.
I have table - attendance
staff_id | in_date | out_date | shift_in | shif_out | in_time | out_time
1 2013-09-10 2013-09-10 06-30-00 15-00-00 07-00-00 15-00-00
2 2013-09-10 2013-09-11 20-00-00 06-00-00 19-00-00 06-00-00
3 2013-09-10 2013-09-11 23-00-00 06-00-00 23-30-00 07-00-00
I need to get result as
staff_id | late_time | early_time | extra_time
1 00-30-00 00-00-00 00-00-00
2 00-00-00 01-00-00 00-00-00
2 00-30-00 00-00-00 01-00-00
Can I achieve the result from mysql query itself or Do I need to use PHP to calculate this ? How can I get this result ?
First of all I would use "DATETIME" in one Column instead of having a DATE column and a TIME column - this makes it easier to compare the Dates and get differences.
Then use "TIMEDIFF()" to compare the times to get the difference.
If your DB-Layout is already set, use ADDTIME to combine the different columns into one DATETIME column like this:
SELECT TimeDiff(AddTime(in_date,shift_in), AddTime(out_date,in_time)) AS late_time
i have table:
Birthday
id | name | date
1 | aaa | 1990-03-02
2 | bbb | 1990-03-12
3 | ccc | 1990-03-25
4 | ddd | 1990-04-25
5 | eee | 1990-04-23
6 | fff | 1990-04-26
7 | ggg | 1990-04-12
How is the best way to SELECT all names where date is 1990-04-xx?
SELECT name FROM table WHERE date LIKE '1990-04%'
Some other answers here are assuming you're storing the date in a datefield, but I assumed by the way it was laid out in your question that it was just a string. Going on that assumption, I knew that using the LIKE operator would let me use a wildcard (the % sign) to search for anything with that year and month. That said, this query will match anything that starts with 1990-04 so there is a possibility with malformed data that you could get some incorrect data (e.g. a date is entered into the database like '1990-041-12')
If it's a DATE value I'd suggest checking if it's between the beginning and the end of the month. If you chop up the date using DATE_FORMAT or some other function you'll lose any chance of optimization:
SELECT name FROM myTable WHERE `date` BETWEEN '1990-04-01' AND '1990-04-30'
If it's a DATETIME value, do this instead to account for values like 4/30/1990 at 9PM:
SELECT name FROM myTable WHERE `date` >= '1990-04-01' AND `date` < '1990-05-01'
If date is a DATE or DATETIME or TIMESTAMP column and you want an index to have chances to be used:
SELECT name
FROM tableX
WHERE date >= '1990-04-01'
AND date < '1990-05-01' ;
An index on (date) would be good. Even better, an index on (date, name).
I got a timestamp record in my msql db which has date and time.
I would to get just the time from it, or just time hour and minute.
How can I split that record?
EDIT here is a picture of my table:
Use MySQL's TIME() function:
SELECT TIME(my_column) FROM my_table
To obtain only the date part, you can use the DATE() function.
Also can try this:
SELECT DATE_FORMAT(now(),'%r') AS timestamp;
| TIMESTAMP |
---------------
| 03:51:43 PM |
SELECT DATE_FORMAT(your_Datetimecolumn,'%r') AS timestamp;
You can also use TIME_FORMAT or DATE_FORMAT to get a specific format e.g. only hh:mm
SELECT TIME_FORMAT( CURRENT_TIMESTAMP, '%H:%i' );
+-------------------------------------------+
| TIME_FORMAT( CURRENT_TIMESTAMP, '%H:%i' ) |
+-------------------------------------------+
| 17:25 |
+-------------------------------------------+
1 row in set (0.00 sec)
EDIT
The function of course works for columns of a table as well
SELECT TIME_FORMAT( myColumn, '%H:%i' ) FROM myTable;
I have a MySQL table that looks something like this:
+-----+------------+
| id | enddate |
+------------------+
| 1 | 2012-06-30 |
+------------------+
| 2 | 2012-07-05 |
+------------------+
| 3 | 2012-07-02 |
+------------------+
On my website I would like to print out all rows who has a date that is between the range:Today's date (I mean not fixed, but the date when the query is beeing run) and 2 weeks ahead.So, using the above example only row 1 and 3 would be printed.Anyone got a solution for this?
I think you need the BETWEEN comparison operator and some simple date functions:
WHERE enddate BETWEEN CURDATE() AND ADDDATE(CURDATE(), INTERVAL 14 DAY);
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#operator_between
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_adddate
You can use
WHERE enddate BETWEEN CURDATE() AND ADDDATE(CURDATE(), INTERVAL 2 WEEK);
Reference