I have the following table :
+----+-------------+
| ID | startDate |
+----+-------------+
| 1 | 2014-10-29 |
| 2 | 2014-10-29 |
| 3 | 2014-10-28 |
| 4 | 2014-10-28 |
| 5 | 2014-10-28 |
| 6 | 2014-10-26 |
| 7 | 2014-10-25 |
| 8 | 2014-10-23 |
| 9 | 2014-10-22 |
+----+-------------+
From this table, I want to get the last 5th day's startDate from current date. The date may not be consecutive I mean in this table my data will not enter everyday but whenever I want to check last 5th day from today it should return like '2014-10-23'. Please help me out.
Use Limit
SELECT startDate FROM table WHERE startDate <= CURDATE() ORDER BY startDate LIMIT 5,1
It means
1)Get records Whose start date is less than current date, and then
2) return one record starting at record 5(means get 5th record).
your query will look something like this
select startDate
from table_name
where startDate <= CURDATE()
order by startDate desc limit 5,1
or
select startDate
from table_name
where startDate <= CURDATE()
order by startDate desc limit 1 offset 5
Related
For example I have below table:
---------------------
| amount | date |
---------------------
| 50 | Day 1 |
| 60 | day 2 |
| 20 | day 3 |
| 25 | day 3 |
| 23 | day 4 |
| 26 | day 4 |
| 15 | day 5 |
---------------------
What I basically want to do is to retrieve item from last 2 days and order the row by max amount. so the result would be like:
---------------------
| amount | date |
---------------------
| 26 | day 4 |
| 23 | day 4 |
| 15 | day 5 |
---------------------
FYI: the date input is in 2018-06-10 14:37:44 mode above is just an example
I tried: SELECT AMOUNT FROM table WHERE AMOUNT=(SELECT MAX(AMOUNT) FROM table) ORDER BY DATE;
But the result I am getting is only | 60 | day 2 | which is the max amount and is not from 2 recent dates;
You could use the DATE_ADD function that gives you a date after a certain time alongside CURDATE() that gives you current date;
SELECT * FROM table WHERE date >= DATE_ADD(CURDATE(), INTERVAL -2 DAY)
ORDER BY amount DESC;
Try this:
SELECT amount, date FROM table WHERE date >= DATE_ADD(CURDATE(), INTERVAL -2 DAY) ORDER BY amount DESC;
-- date > DATE_ADD(CURDATE(), INTERVAL -2 DAY) will give you all rows within 2 days
-- ORDER BY amount DESC will give you ordered by amount descending
I know it's possible to get yesterday records, most common way using SUBDATE(CURDATE(), 1) or maybe simply use CURDATE() - 1 and use LIMIT and ORDER to retrieve the last record of yesterday.
But here, I need to get the last record of yesterday in the first row and the rest will be all records of today. I need to run this within single query.
For example, I have following records in one of my table:
--------------------------------------------------
| value | created_at |
--------------------------------------------------
| 70 | 1/1/2017 |
| 300 | 1/1/2017 |
| 100 | 1/1/2017 |
| 235 | 1/2/2017 |
| 45 | 1/2/2017 |
--------------------------------------------------
The created_at column is a timestamp, if today is 1/2/2017 (2th January 2017) then the result of the query should be:
--------------------------------------------------
| value | created_at |
--------------------------------------------------
| 100 | 1/1/2017 |
| 235 | 1/2/2017 |
| 45 | 1/2/2017 |
--------------------------------------------------
So far, I only able to retrieve the records of today with following query:
SELECT * FROM my_table WHERE created_at >= CURDATE();
What query I need to accomplish this?
Hoping you have id as primary key
select * from
(select
*
from
tbl
where date(created_at) =date(DATE_ADD(now(), INTERVAL -1 DAY))
order by id desc limit 0,1
)tmp
UNION
select * from tbl where date(created_at)=date(now())
I am having a table with seperate date and time column. I need to select all data from my table checking two condition
1.records with current date(todays date).
2.records with custom time range.
This is my table structure
+------------+------------+---------------------+-------------------+--------------------+
| id | item | description | bill_date | bill_time |
+------------+------------+---------------------+-------------------+--------------------+
| 1 | x | test | 2016-04-15 | 12:05:00 |
+------------+------------+---------------------+-------------------+--------------------+
| 2 | y | test1 | 2016-04-15 | 01:10:44 |
+------------+------------+---------------------+-------------------+--------------------+
| 3 | z | test2 | 2016-04-16 | 05:10:10 |
+------------+------------+---------------------+-------------------+--------------------+
I could select time range using this mysql query
SELECT * FROM `bill_item` WHERE `bill_time` BETWEEN '12:00:00' AND '06:00:00'
this returns 2,3 record now I need to check if the record is todays record. How to do this?
It is possible to have multiple conditions in the WHERE clause. You can use CURRENT_DATE() or NOW() to get only todays entries.
SELECT *
FROM `bill_item`
WHERE (bill_time BETWEEN '12:00:00' AND '06:00:00')
AND bill_date = CURRENT_DATE()
See the official documentation for more Date and Time Functions
So I have a query that correctly displays the number of registrations for the last 12 months. Here is display:
Registrations per month for last 2 years
1--17
2--12
3--17
4--8
5--9
6--8
7--15
8--20
9--12
10--14
11--13
12--14
But since im running this in say June, the last mont I need to say the readable date May and not '1'. I want instead:
May--17
Apr--12
March--17
.
.
.
Here is my current MYSQL:
SELECT MONTH(create_date) as month , COUNT(create_date) as count
FROM `users`
WHERE create_date >= NOW() - INTERVAL 1 YEAR
GROUP BY MONTH(create_date)
I assumed I just have to use FORMAT_DATE() on the GROUP By as:
GROUP BY FORMAT_DATE(MONTH(create_date, '%M'))
And that would give me my readable month, but the sql statement reports it is not correct. Anyone know how to accomplish this?
Try this:
SELECT DATE_FORMAT(create_date, '%M') AS month, COUNT(create_date) AS count
FROM users
WHERE create_date >= NOW() - INTERVAL 1 YEAR
GROUP BY MONTH(create_date);
The result will be:
+-----------+-------+
| month | count |
+-----------+-------+
| January | 1 |
| February | 1 |
| March | 1 |
| April | 1 |
| May | 2 |
| June | 2 |
| July | 1 |
| August | 1 |
| September | 1 |
| November | 1 |
| December | 1 |
+-----------+-------+
You can use STR_TO_DATE() to convert the number to a date, and then back with MONTHNAME()
SELECT MONTHNAME(create_date(6, '%m')) as month , COUNT(create_date) as count
FROM `users`
WHERE create_date >= NOW() - INTERVAL 1 YEAR
GROUP BY MONTH(create_date)
So I have a events table with 4 records in it:
| id | date | active_from |
| 1 | 2015-01-27 | 2015-01-22 |
| 2 | 2015-01-30 | 2015-01-24 |
| 3 | 2015-01-29 | 2015-01-27 |
I'm trying to create a simple query just to grab the records when today is lower than or equals to date and today is greater than or equals to active_from.
So when its today 2015-01-26 I want to have the records with id 1 and 2,
because the event is active and is yet to come.
Currently I have this query:
select * from `events` where `active_from` <= '2015-01-26' AND `date` >= '2015-01-26'
But it's not working... I must be looking over something here.
You are putting the < and > the wrong way. Have a try with this one:
SELECT * FROM events WHERE CURDATE() BETWEEN active_from AND `date`