I'd like to add a column to my database which displays the amount of time in days since a user signed up.
Currently I have a field which displays the date they signed up in unix.
Is it possible for the new column to increase its fields by 1 each day?
You can basic SQL to get this information for you dynamically. DATEDIFF() will be what you need:
SELECT
DATEDIFF(CURRENT_DATE, FROM_UNIXTIME(date_signed_up)) AS days_since_signup
FROM
tablename
current time - timestamp_of_registration:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
SELECT DATEDIFF(CURTIME(), SIGNUP_DATE);
Related
I have a problem related to MySQL query, I use WAMPServer.
I have data in database which have range of dates but when I select data for example
select * from CHD WHERE addtime>='2018-06-15' and addtime<='2018-06-21';
It displays data from '2018-06-15' to '2018-06-20', data of 2018-06-21 are not displayed even if I do
select * from CHD where addtime='2018-06-21';
is not working
Please anyone can help me
This assumes that your column is of type datetime.
The shorthand version of your date in the filter clause is assumed to be at midnight of the date. Your values that you are attempting to retrieve have times after midnight of that date. You either need to define a timestamp along with the date, or you need to filter by the day after for less than equal to or the day before for greater than equal
I currently have my database updating the timestamp when new records are inserted. It formats like this:
2017-08-24 15:48:30.189182
My question is: how do I delete records that are over, suppose, 3 days old?
I'm assuming I can run a cron job nightly to remove the records older than 3 days. Can someone give me the format for it? I'm not sure how to work with the timestamp data.
Thank you for your help!
different database has different way to compute time difference between two days...
MySQL: DATEDIFF(date1,date2);
Oracle: select date1 - date2 from dual;
and so on...
Assuming the column is of type DATE, you can run a query that deletes all columns where the difference between today and your column is greater than 3.
Replace table with your table, and date_column with your column-name. Again, this assumes that you've got columns that is of the DATE-type, and not a VARCHAR or something.
DELETE FROM table WHERE DATEDIFF(CURDATE(), date_column) > 3
SQLFiddle
MySQL Date functions
I'm making a PHP based site designed to display line graphs based on data over time. Where the user selects a time range and gets a graph corresponding to what was selected.
The problem is that to calculate any given point, I need to know the previous record. I have no way of knowing when it was, it may have been an hour or a week before hand, but it could have been a minute.
So is there anyway, from within SQL, to specify a time range and one record before that?
You can do another query that gets the last record before the time range:
SELECT *
FROM yourTable
WHERE time < #start_time
ORDER BY time DESC
LIMIT 1
You can combine this with the original query using UNION.
I'm new to MySQL and PHP but was wondering if someone could help me with a little project I'm doing for my boss.
I have a SQL database (MyDB) and a table in there (mytable) with two columns - the first column (index) is an auto-incrementing integer from 1-10, the second column (date) has different dates and timestamps in the format of Year-month-day time 2013-04-12 1326
I'm trying to create a simple PHP page that first gets the current date (easy enough) then looks at the table and shows the number of rows that fall within yesterday's date. For example, if I have 3 rows with 2013-04-11 XXXX and 2 rows with 2013-04-12 XXXX (and today is the 12th April 2013) the page will display 3. (The time is not important but we can't remove it from the table as it's auto created by one of the other staff's programs and he refuses to change it).
So far I've got my php page, done a connection to the DB and defined two variables:
$startdate = date('Y'."-".'n'."-".'d'." "."0000");
$enddate = date('Y'."-".'n'."-".'d'." "."2359");
As the timestamp doesn't matter I've gone for the min/max possible on the variables. I realise this will only give the current date, trying to work out how to get it to display the previous day as the date in the variable.
Now I'm trying to create a sql query that will count the number of rows where the date field falls within the startdate and enddate variables (-1 day) but not too sure where to start or how this would look. I then need to output this as a variable in PHP so I can echo it later in the page.
Anyone able to point me in the right direction? Hope any of this makes sense.
You could write a query with no params to do this (if its always just yesterday).
SELECT * FROM <table>
WHERE DATE_FORMAT(<date column>,'%j-%Y') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 DAY), '%j-%Y');
Date functions in the where clause might not be super awesome performance wise
I have two field in a database table departureDateTime and arrivalDateTime and the values like
departureDateTime=03/18/2012 1:05 PM
arrivalDateTime=03/18/2012 3:15 PM
I have hundreds of records in the table.
I need to sort and display according to the duration from these two time. I know to calculate the duration from two dates
Duration=(strtotime($arrivalDateTime') - strtotime($departureDateTime))/3600
But how I write a mysql query to sort and display these 100 records from database
Does any one any idea?
Thanks
Change your departureDateTime to DATETIME instead of varchar or whatever you are currently using. Then you would use something like this:
SELECT other, stuff, TIMEDIFF(departureDateTime, arrivalDateTime) as theDifference FROM myTable ORDER BY theDifference ASC LIMIT 0, 100
If you kept your date and times in a date time column type then you wouldn't have this problem. Consider changing.
There are mysql functions to calculate dates and time too
http://dev.mysql.com/doc/refman/5.6/en/datetime.html
If not, you can do sorting in PHP using usort.