How to show next date using mysql - php

I have an example
id projectid date
1 2 2015-04-19
2 2 2015-04-19
3 2 2015-04-19
4 2 2015-04-19
5 2 2015-04-21
6 2 2015-04-21
7 2 2015-04-21
8 2 2015-04-22
9 2 2015-04-22
10 2 2015-04-22
I want to show next upcoming stored date using 2015-04-19 date, i.e next date is 2015-04-21, using mysql query
id projectid date
5 2 2015-04-21
6 2 2015-04-21
7 2 2015-04-21

I have written query for you. I hope it will resolve your problem.
QUERY
SELECT * FROM table
WHERE `date` = (SELECT `date` FROM table
WHERE `date`>'2015-04-19' ORDER BY `date` ASC LIMIT 1);

Use DateAdd Function in Select Statement like below
DATEADD(d,1,(CASE table.date as Date))

Basically to display next date older than 2015-04-19 from your database, this should be enough:
SELECT * FROM table WHERE date > '2015-04-19' ORDER BY date ASC LIMIT 1

SELECT * FROM table WHERE date > '2015-04-19' ORDER BY date LIMIT 1
UPDATE
You wrote that you want to show next upcoming stored date, but looking at your example output and your comment it looks like you want to get all records with the next date.
SELECT * FROM table WHERE date=(
SELECT date FROM table WHERE date > '2015-04-19' ORDER BY date LIMIT 1
)
SQL Fiffle example, use it as a copy paste solution.

Sub query will return the next date from which results will come according to that date only.
SELECT * FROM tableName WHERE date=("SELECT date FROM table WHERE date > '2015-04-19' ORDER BY date LIMIT 1")

Related

how can i get the nearest date values from the current date in mysql

Events Table
This is my events table what i need is i need to get the nearest date values from current date.
Current Table
--------------- --------------------------------------
events future_dates Conversion_Rate
--------------- --------------------------------------
One 1 01-08-2015 1000
One 1 03-08-2015 1000
One 1 06-08-2015 1000
One 1 07-08-2015 1000
One 1 10-08-2015 0
Two 1 13-08-2015 0
Two 1 14-08-2015 0
Two 1 16-08-2015 0
------------------------------------------------------
My Expected Result
--------------- --------------------------------------
events future_dates Conversion_Rate
--------------- --------------------------------------
One 1 07-08-2015 1000
One 1 10-08-2015 0
Two 1 13-08-2015 0
Two 1 14-08-2015 0
Two 1 16-08-2015 0
------------------------------------------------------
What i have tried so far
select * from events where CURDATE() < Conversion_Rate
What i need is i need to get the nearest date from current date and where conversion rate != '0'.How can i get the expected result in mysql ?
If I understand correct you want to fetch a latest record before current date which has a non zero conversion rate along with all other records from current date and later.
If so, a UNION may help you as shown in the following example:
(
select * from table_name
where conversion_rate <> 0
and future_dates < current_date
order by future_dates desc limit 1
) -- this fetches pre cur date recs
union
( -- this fetches cur and post cur date recs
select * from table_name
where future_dates >= current_date
)
Try this
SELECT *
FROM Table
WHERE Conversion_Rate != 0
ORDER BY ABS(DATEDIFF(future_dates, NOW()))
LIMIT 10
or
SELECT *
FROM table
WHERE Conversion_Rate != 0
ORDER BY ABS(now() - future_dates) DESC
limit 10
Future_date should be '13/08/2015'. This record should be display today.
I think you may be want this query.
select * from events where CURRENT_DATE - INTERVAL 5 DAY <=future_dates and Conversion_Rate != 0
select *
from events
where Conversion_Rate != '0'
order by ABS(DATEDIFF(CURDATE(),future_dates )) asc

Same Query with different where clause

I have a table as:
date passed failed subject
2015-2-1 2 1 maths
2015-2-1 3 2 cs
2015-3-1 1 2 maths
2015-12-1 2 1 maths
I have a form to select the start date and end date.
for eg:If I select startdate = 2015-2-1 and enddate=2015-3-1
my output should be:
date passed failed
2015-2-1 5 3
2015-3-1 1 2
output should contain the total no of passed and failed (irrespective of subjects) for same months as shown above.
This is the query which I used:
startdate=2015-2-1 and enddate=2015-3-1
SELECT SUM(passed) ,SUM(failed) FROM student_log WHERE
(DATE(`date`)='2015-2-1')
union
SELECT SUM(passed) ,SUM(failed) FROM student_log WHERE
(DATE(`date`)='2015-3-1')
It works fine But if my end date is December I should repeat my union query till December...
Please suggest an alternative.
What about:
SELECT SUM(passed), SUM(failed), DATE(`date`)
FROM student_log
WHERE DATE(`date`) >= '2015-2-1' AND DATE(`date`) <= '2015-3-1'
GROUP BY DATE(`date`);
This would find all tests between two dates and give you a count from each day.
You dont need separate queries or join or union for this
SELECT SUM(`pass`) as total_pass, SUM(`fail`) as total_fail
FROM pass_fail
WHERE (`date` = '2015-2-1' or `date` = '2015-3-1' )
GROUP BY `date`
Output
Refer Group By
this query :
Select date,sum (passed),sum(failed) from Results group by date
returns the following results for me :
date passed failed
2015-02-01 5 3
2015-03-01 1 2
2015-12-01 2 1
In other words: The group by clause will display the data per Month as you need it.

Mysql Query for counting rows with specific days difference

Going to make a line graph. I want to query count with difference of 5 days.
Suppose if I have following rows:
booking_date
2015-02-1
2015-02-3
2015-02-5
2015-02-6
2015-02-6
2015-02-9
2015-02-10
2015-02-15
2015-02-17
2015-02-23
2015-02-28
In above table column it contains date. Now How can I do mysql query so that it can return with difference of 5 days like:
1 => 3 // count of date between 2015-02-1 & 2015-02-05 is 3
2 => 4 // count of date between 2015-02-06 & 2015-02-10 is 4
3 => 1 // count of date between 2015-02-11 & 2015-02-15 is 1
4 => 1 // count of date between 2015-02-16 & 2015-02-20 is 1
5 => 1 // count of date between 2015-02-21 & 2015-02-25 is 1
6 => 1 // count of date between 2015-02-26 & 2015-02-30 is 1
Is any direct way to query like above. I am not so good at mysql. But can do php nicely.
You can do the following to get everything in the same query.
First of all get the unix timestamp for the date you want to start grouping in 5 days. In your example that would be 2015-02-01 -> 1422748800
Then the query would be the following:
SELECT COUNT(*), FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5)) as FiveDayPackNumber from tbl GROUP BY FLOOR((UNIX_TIMESTAMP(booking_date) - 1422748800)/ (60*60*24*5))
Haven't tested it so it may require some tweaking but you can get the idea: It will group them by the number of 5-days-packs that passed since your initial date, starting at 0.
Do you mean to do something like:
SELECT COUNT(1) FROM {table_name} WHERE booking_date > 2015-02-01 AND booking_date < 2015-02-05
?
(completely off memory so you might need to slightly alter it)
SELECT count(*)
FROM tbl
WHERE booking_date >= start_date
AND booking_date <= end_date

Trying to find out subscription end month notification

Hi i have this mysql table
id amount substart years subend
1 200 2012-01-10 1 2013-01-09
2 250 2012-02-15 2 2014-02-14
3 100 2012-02-11 1 2013-02-10
4 260 2012-03-22 3 2015-03-21
What i want is that to give notification a month before the end date. The current query is:
select count(subend) as count,curdate()
from subdur where status='active'
and (date_sub(subend,interval 1 month))>=curdate()
and (date_sub(subend,interval 1 month))<date_add(curdate(),interval 1 month)
order by subend;
The query is not giving me proper answer.
Thanks in advance
Try this::
select
count(subend) as count,
curdate()
from subdur
where status='active' and
subend BETWEEN (date_sub(curdate(),interval 1 month)) and curdate()
order by subend
Another way would be using date_diff:
select count(subend) as count, curdate()
from subdur
where status='active'
and date_diff(subend, curdate()) = 30 // >= 30 days or more, = 30 days exact
order by subend
;

curdate interval for a int field?

I want to select records from the last 3 years...
the following use to work when the table column type 'released_year' was a date
$query = 'SELECT
album.album_id,
album.title,
album.released_year,
FROM album
WHERE album.released_year >= ( CURDATE() - INTERVAL 3 YEAR )
ORDER BY album.released_year DESC, album.title';
but the table column type change and it's now a type smallint to handle only the 4 digits of a year.
How do i select records from the last 3 years now?
... album.released_year >= year(CURDATE()) -3 ...
ugly as you're not using real dates, but it will work for a while

Categories