This question already has answers here:
SQL Query to show nearest date?
(5 answers)
Closed 3 years ago.
I want minimum Data From MySql DataBase. In my database Column's DataType is VARCHAR and i inserting data is like as "2011-03-01 09:00" (yyyy-MM-dd hh-mm). Now i need Minimum Date from my Database. How I can do this.? Please Help Me.
Thanks in advance.
Convert the col into a unix timestamp, get the current date as a unix timestamp, get the difference, make it a positive number, order by that number, return the first row.
SELECT * FROM tablename ORDER BY ABS(UNIX_TIMESTAMP(dateCol) - UNIX_TIMESTAMP(NOW())) ASC LIMIT 1
Edit: from your comments, you need the next row (ordered by date):
SELECT * FROM tablename WHERE UNIX_TIMESTAMP(dateCol) > UNIX_TIMESTAMP(NOW()) ORDER BY UNIX_TIMESTAMP(dateCol) ASC LIMIT 1
sort the table by your date (string) column ascending.
limit the rows to the first. get you date column.
then you have the smallest date.
no conversions are needed.
this is only possible, when you are using this format: yyyy-MM-dd hh-mm
but you are doing this, already
select datecol from table order by 1 limit 1,1
maybe the limit in the sample is wrong - i don't know if mysql starts at 1 or 0
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 5 years ago.
I have a very simple table called MYTABLE:
TSTAMP VARCHAR(20), VALUE VARCHAR(20)
The timestamp is like yyyy-mm-ddTHH:MM:SS.zzzZ (ex: 2017-04-25T12:23:00.000Z).
Value is a float.
I know this table model is just bad but it has been done many years ago by someone else who obviously didn't know what he/she was doing.
I am trying to write an efficient query to get the min(VALUE), the max(VALUE) and their TIMESTAMP over intervals of time (ex: min/max of each minute).
I am able to get the min and max value with the following query but I cannot see a way to get their timestamps.
SELECT MIN(tstamp)
, MAX(tstamp)
, MIN(value) minVal
, MAX(value) max
, ABS(TRUNCATE(((UNIX_TIMESTAMP(tstamp)*1000+SUBSTR(tstamp,-4,3)) - 1493115780000)/20000,0)) intervalNumber
, tstamp
FROM mytable
WHERE tstamp BETWEEN '2017-04-25T12:23:00.000Z' AND '2017-04-25T12:24:00.000Z'
AND NOT tstamp = '2017-04-25T12:24:00.000Z'
GROUP
BY intervalNumber;
WHERE 1493115780000 is the result of:
SELECT UNIX_TIMESTAMP('2017-04-25T12:23:00.000Z')*1000+SUBSTR('2017-04-25T12:23:00.000.000Z',-4,3);
*EDIT BECAUSE I WAS NOT CORRECTLY EXPLAINING
What i get is for each interval:
first timestamp , lasttimestamp ,min value , maxvalue , internval number, first timestamp
What i want is:
timestamp of min value, timestamp of max value, min value, max value, interval number
I am using Mysql 5.5.
Any help would be appreciated :)
It looks like an university tutorial but this is now too far behind me :(
Assuming ABS(TRUNCATE(((UNIX_TIMESTAMP(TSTAMP)*1000+SUBSTR(TSTAMP,-4,3)) - 1493115780000)/20000,0)) as intervalNumber is correct.
I think that you're looking for something like :
-- Min value
SELECT top 1 min(TSTAMP), VALUE,
ABS(TRUNCATE(((UNIX_TIMESTAMP(TSTAMP)*1000+SUBSTR(TSTAMP,-4,3)) - 1493115780000)/20000,0)) as intervalNumber
FROM MYTABLE
WHERE TSTAMP between '2017-04-25T12:23:00.000Z' AND '2017-04-25T12:24:00.000Z'
AND NOT TSTAMP='2017-04-25T12:24:00.000Z'
GROUP BY VALUE desc
-- Max value
SELECT top 1 max(TSTAMP), VALUE,
ABS(TRUNCATE(((UNIX_TIMESTAMP(TSTAMP)*1000+SUBSTR(TSTAMP,-4,3)) - 1493115780000)/20000,0)) as intervalNumber
FROM MYTABLE
WHERE TSTAMP between '2017-04-25T12:23:00.000Z' AND '2017-04-25T12:24:00.000Z'
AND NOT TSTAMP='2017-04-25T12:24:00.000Z'
GROUP BY VALUE asc
The best solution would be to clean your table in the first place by dumping all in a temp table, recreate the table with correct structure and dump back with conversions. Your request shall be simplier after that.
What is the best way to format a date in a MySQL query?
I am currently running the query below which selects the data but doesn't order the information correctly, I presume this is due to the current format of the 'updatetime' row.
SELECT * FROM updates WHERE udcode='Remote Connection' ORDER BY updatetime DESC LIMIT 20;
The current format is as follows:
31/03/2015 13:41:45
How should this date be formatted in order for the ORDERING to work correctly?
Thanks in advance.
Use:
ORDER BY DATE_FORMAT(updatetime, '%Y-%m-%d %H:%i:%S') DESC
you can change the format of your date in MySQL with DATE_FORMAT
SELECT *, DATE_FORMAT(`updatetime`, '%Y-%m-%d %H:%i:%S') AS mydate FROM updates WHERE udcode='Remote Connection' ORDER BY mydate DESC LIMIT 20;
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
You are doing this correctly in the Query, however if you are getting undesired results then there is a possibility that the data type for your updatetime attribute is not correct.
You should use datetime as the data type so that the query is correctly able to distinguish that the information is a date and knows how to order it properly. For example if you are using a varchar it will think that the first digit reading left to right is the most significant rather than recognising the difference and relationship between days, months, years and time.
If you dont have the correct datatype you would either have to include formatting in a bloated query or you would end up with all the days from 10-19 ordered before the 20-29 and then followed by the days 3-9. In this situation the 30th would be considered to be ordered before the 4th for example.
There will be little relevance by the time you are ordering months or years as the day units will have mixed everything up
You can try following changes
Change date format to
yyyy-mm-dd hh:mm:ss
And edit query to
SELECT * FROM updates WHERE udcode='Remote Connection' ORDER BY `updatetime` DESC LIMIT 0, 20;
I have a table
id |value |date
-------------------
1 |2.8 |28-3-14
2 |2.9 |28-7-14
3 |3.9 |20-1-14
in this table i need to get the value of 21-3-14.
but if value or object is not present for that then query get output of 20-1-14 directly without one by one search object by minus date by 1 day.
if any one know about this please give me suggestion.
You just need to sort by date
SELECT value FROM table WHERE date<='21-3-14' ORDER BY date DESC LIMIT 1;
Based on your table it should print:
2.8
Assuming the date 21-3-14 wasn't there, it should print:
3.9
try this,
SELECT
*
FROM
<tablename>
WHERE
STR_TO_DATE(`date`,'%d-%m-%y') <= STR_TO_DATE('YOUR_DATE','%d-%m-%y')
ORDER BY
`date` DESC < LIMIT 1 >
It is recommended to store date in date format i.e. < yyyy-mm-dd >
you may refer,
PHP mysql insert date format
This question already has answers here:
Retrieving the last record in each group - MySQL
(33 answers)
Closed 8 years ago.
I have a table with date and Etotalday.
What I want to select is the last value of the last 7 days.
Many solutions I looked for gives the highest value of a day which is not always the same as the last value.
In my case table is build with values and the day starts with the highest value of the day before and at about 06.00 new values are added.
Is there somebody who can gives me a hint how to do this?
thnks
Lookes like duplicate question but I want the last value not the highest value and not the max value.
You can do this with order by and limit
select t.*
from PacData t
where date >= now() - interval 7 day
order by date;
limit 1;
If you want to return the last value for each of the past seven days (which might be the intent of the question), then here is one method:
select t.*
from PacData t
where not exists (select 1
from PacData t2
where date(t2.date) = date(t.date) and t2.date > t.date
);
This says: "Get me all records from the table where there is not record on the same date with a larger value from the date time column". By the way, date is a lousy name for a column that stores both a date and a time.
How can add 1 year to a date value stored in the DB as varchar (format: "2011.03")?
I'm trying with this, but returns NULL :(
DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m')
Thank you very much!
edit.: this is the query i want to use in:
SELECT DISTINCT column FROM table WHERE column BETWEEN '2011.03' AND DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m') ORDER BY column DESC
("2011.03" is a parameter value and comes outside)
What you are trying is giving correct result
Try :
SELECT DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03', '%Y.%m'), INTERVAL 1 YEAR),'%Y.%m')
it's giving 2012.03.
Edit:
You can use below trick to make it workable in mysql version > 5.0
Add month with date because in Mysql version > 5.0 str_to_date() sometimes would return NULL if the %D format specifier was not the last specifier in the format string input.
Try below:
SELECT DATE_FORMAT(DATE_ADD(STR_TO_DATE('2011.03.01', '%Y.%m.%d'), INTERVAL 1 YEAR),'%Y.%m')