How can I search a column in MySQL containing unix timestamps? - mysql

I have mysql table called user_log that contains something like:
userid created_at
1 1388514600
2 1391193000
I want to get the record using exact date. For example I have the created_at which is 2013-12-31
SELECT * FROM user_log WHERE created_at = UNIX_TIMESTAMP('2013-31-12');
But record is not selected, I don't know what was the problem in that query. So how can I get the record from unix timestamp column in mysql using date?

To know the exact value in dateformat, you can do :
SELECT FROM_UNIXTIME(created_at) FROM user_log;
That said, the value 1388514600 is not just 2013-12-31 but it's actually 2013-12-31 18:30:00;
So to search by just date, you can try this:
SELECT FROM_UNIXTIME(created_at) AS dt
FROM user_log
HAVING date(dt)='2013-12-31';
Fiddle here

Related

SQL Query for returning the latest date

If I consider the following:
Customer | Date
100 | 09-06-2021
100 | 17-03-2020
I am trying to find out the most recent date from the above, by using: select MAX(Date) from table, and it is returning 17-03-2020, which is wrong.
How do I get the most recent date as 09-06-2021?
What is wrong is that you are storing the date as a string, not a date.
You can fix your immediate problem by doing:
select max(str_to_date(date, '%d-%m-%Y'))
from t;
But you should really fix the data. Start by putting the date in a canonical format:
update t
set date = str_to_date(date, '%d-%m-%Y');
Then modify the column to the correct data type:
alter table t modify column date date;

get recent most Row specific to date in mysql

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

SQL order by and SQL date

I have 2 doubts related to SQL query's
id name update
1 some 2013-05-03
2 som 2013-05-08
3 smee 2013-06-05
How can i list items on a particular month (I want all records,year and date will not be specified I just want to check the month)
How can I arrange name in alphabetic order and arrange it as groups of names such as (limiting number of records =10)
Array A = names starting with A
Array B = names starting with B
The easiest way, to fetch MONTH from a DATE or DATETIME type of fields is to use the MySQL's date-time function MONTH().
For your query, it shall be:
SELECT *
FROM tblName
WHERE MONTH( `update` ) = <month Number such as 5>
The second would need a more complex query. I'd rather use php to do the grouping better(as I've more control over that language).
You can simply use datatype of the field as DATE or you can store any date as unix timestamp and then convert it whenever you want to show it.
Example: 1363979714 (ISO 8601:2013-03-22T19:15:14Z)
If you want list items on a particular date, you can write your query like this:
Month:
Select * from tableName where update like '%-5-%'
day:
Select * from tableName where update like '%-16'
year:
Select * from tableName where update like '2013-%'

MySQL - Select most recent date out of a set of several possible timestamps?

I would like to know if this is possible using a mysql query:
I have a table called updates.
In the updates table I have 2 columns, id and timestamp.
I have 5 rows each with the same id but with different date timestamps. An example of this timestamp would be the value: 2012-08-04 23:14:09.
I would like to select only the most recent timestamp value out of the 5 results. This could also be explained by saying that I would like to select the value that is closest to the current date and time. How could I do this?
SELECT id, timestamp FROM updates ORDER BY timestamp DESC LIMIT 1
have you tried SELECT MAX(timestamp) ?

how to count records with newest date only

How do I modify this MySQL query to only count leadIDs from table leads where column 'Date' contains the newest (youngest) date?
SELECT COUNT(leadID) as accepted FROM leads WHERE change like '%OK%'
The problem is that leadID can have multiple instances in table leads. The original query result is "4" because of one duplicate. The correct result is "3".
The date is stored in this format: 2011-10-26 18:23:52. The result should take hours and minutes into consideration when determining the youngest date.
TABLE leads:
leadID | date | change
1 | 2011-10-26 18:23:52 | BAD
1 | 2011-10-26 17:00:00 | OK
2 | 2011-10-26 19:23:52 | OK
3 | 2011-10-26 20:23:52 | OK
4 | 2011-10-26 21:23:52 | OK
5 | 2011-10-26 22:23:52 | BAD
I think this is what you're looking for:
select count(distinct l1.leadId) as accepted from leads l1
left join leads l2
on l1.leadId = l2.leadId and l1.date < l2.date
where l2.date is null and l1.`change` like '%OK%'
You must decide what you mean by newest date: the single latest? yesterday? today?
if yesterday, then add this to your query clause
select * from mytable where date >= date_sub(now(), interval 1 day)
if you are using oracle database you can use max() function to extract newest date from the table, further to check with the table for this newest date :-
SELECT COUNT(leadID) as accepted FROM leads WHERE change like '%OK%'
and date_col = (select max(date_col) from leads)
I am assuming that with newest date your mean is about newest in the table data..
changes :- as per changes in question and as per mentioned in commends ..
I think you want to take newest date among the records having "change" column value like '%OK%' and want to count distinct leadId
please try the following query-
SELECT COUNT(distinct leadID) as accepted FROM leads WHERE change like '%OK%'
and date_col = (select max(date_col) from leads WHERE change like '%OK%')
You can try (in case your date is a int like return by time() function)
$sql = "SELECT COUNT(leadID) as accepted FROM leads WHERE change like '%OK%' ORDER BY Date DESC LIMIT 1"
You will only extract the newest entry.
Edit: This shouldalso works for your date format YYYY-MM-DD hh:mm:ss
Edit 2: Okay, I did not understood your question.
You have a table lead: leadid date
You want to count the number of row for the newset date.
Like another pointed out you can use the MAX operator:
SELECT COUNT(distinct leadid)
FROM LEAD AS l,
( SELECT MAX(Date) mdate FROM Lead ) AS MaxDate
WHERE l.date = MaxDate.mdate
AND l.change like '%OK%'