get recent most Row specific to date in mysql - 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

Related

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

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

Mysql grabbing correct price depending on date

I have a table that looks like the following:
The query below obviously returns all records in the table
SELECT * FROM pricing
My problem I have is that I want to display the correct price based on todays date. I know I can grab todays date using CURDATE() but how do I grab the row that shows the price of 70.00 as this is the correct price until today is equal to or greater than 2017-02-01?
Thanks in advance.
John
select price from table where product = YourProduct and date <= YourDate order by date desc limit 1

Ordering a mysql table by date when the column is declared varchar

I need to sort a table by date (descending), but all columns in the table are varchar, so I need to manipulate the data on the fly for sorting it correctly.
date sales
10/09/2014 100
13/09/2014 250
30/08/2014 200
Is that possible without altering the table? So the result will be like below, newest dates first?
date sales
13/09/2014 250
10/09/2014 100
30/08/2014 200
Like pseudocode
SELECT * FROM table ORDER BY (CONCAT(REGEXP(date, '[0-9]{4}'),
REGEXP(date, '/[0-9]{2}/'), REGEXP(date, '^[0-9]{4}/')) DESC
I think I need to use substring_index somehow, because regexp just returns 1 or 0, not the actual value found.
You need to convert your varchar-stored date objects into DATE objects, then use them to order.
This you can do on the fly like so
ORDER BY STR_TO_DATE(date,'%d/%m/%Y') DESC
But performance is going to be horrible. For best results store your dates in a DATE column in your table.
you can use STR_TO_DATE
SELECT *
FROM Table1
ORDER BY STR_TO_DATE(date, '%d/%m/%Y') desc,
sales desc

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) ?