mysql group_concat showing all dates - php

I have 2 tables:
profile userid, fname,lname
schedule: userid, date, start_time, end_time
Here is my query .
select *, group_CONCAT(distinct(date),'_',start_time,'_',end_time)
as dateformat FROM profile, schedule WHERE profile.userid =
schedule.pid
This returns all dates from the table. But what I am trying to do is return only certain days like:
date >= DATE(NOW()) and date <= date(now() + 2 day)
So today tomorrow and the day after.
No matter what I try it just returns all dates in the schedule database. I need this to return 2 rows userid 5 has 1 row in profile but 5 rows in schedule and userid 6 has 1 row in profile and 2 rows in schedule the query works fine its just returning all dates and not the dates within the
"date >= DATE(NOW()) and date <= date(now() + 2 day)"

did you try GROUP BY?
like WHERE profile.userid = schedule.pid GROUP BY date

select profile.userid,schedule.userid, group_CONCAT(distinct(date),'_',start_time,'_',end_time) as dateformat FROM profile, schedule WHERE profile.userid = schedule.userid and date >= DATE(NOW()) and date <= date(now() + interval 2 day);
That did it for now!! Now I can use the dateformat variable in php to parse out my results.
kanishka panamaldeniya - thanks for your suggestion!

Related

Sql query to get row from date filled

I want to get data from training_course table where current date minus 5 days is equal to training_end_date. Training_end_date is my field in the table.
Thanks
You seem to want:
select *
from training_course
where training_end_date = current_date - interval 5 day
Or, if your dates have time components, you maybe want:
select *
from training_course
where training_end_date >= current_date - interval 5 day and training_date < current_date - interval 4 day

MYSQL Last month Query is not returning first 9 days

I am querying records from the last calendar month. As it is February, it should return all the records that were added on January this year.
My Query:
`SELECT * FROM table_name WHERE date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1
DAY) AND date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1
MONTH)), INTERVAL 0 DAY) AND campaign = '$campaign' ORDER BY date
ASC`
It returns some records but skips the first 9 days. It starts showing records from 10th of the previous month. What am I missing here?
check your date field type and make sure you have not mistaken it with varchar.
SELECT * FROM table_name WHERE
(MONTH(date) = (MONTH(NOW()) - 1) AND YEAR(date) = YEAR(NOW()))
OR
(MONTH(date) = 12 AND MONTH(NOW())=1 AND YEAR(date) = (YEAR(NOW()) - 1) AND campaign = '$campaign' ORDER BY date
ASC`
Try To Get Data in step by step like,
First, you should try below query.
SELECT Date, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AS StartDate, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY) AS EndDate FROM MyTable
Second, If First Step give right date then get your data by directly writing your date rather than DATE_ADD function in where clause.
Third, If These will Give you write DATA then try to fetch data using DATE_ADD function.
Replay If you will get solution.
SELECT * FROM table_name WHERE date between DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 MONTH) AND DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 DAY) AND campaign = '$campaign' ORDER BY date ASC

mysql query get 1st specific day between 2 dates

How do I get the first Sunday between 2 dates?
i have only 2 fields in myTable (dt_from and dt_to)
dt_from = '2016-08-6';
dt_to = '2016-08-19';
SELECT firstsunday FROM myTable BETWEEN dt_from AND dt_to;
How about getting the first Sunday just after from date itself?
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) from myTable as A;
http://sqlfiddle.com/#!9/7fce5
If Sunday isn't between the dates:
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A where DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) between A.date_from and A.date_to;
http://sqlfiddle.com/#!9/83d0f0/4
A little bit shorter:
SELECT firstsunday from (SELECT A.date_from, A.date_to, DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A) as B where B.firstsunday between B.date_from and B.date_to;
http://sqlfiddle.com/#!9/6adab3/1
Try this:
SELECT dt, DAYNAME(dt) as day
FROM myTable WHERE dt BETWEEN date1 AND date2
ORDER BY dt
WHERE day = "Sunday"
LIMIT 0,1
assume you only need to sue the db to get the date range so you dont want any king of query.
<?php
echo date('m/d/Y', strtotime('next Sunday', strtotime('2016-08-6')));
you can add some validation to make sure its before your end date

how to select last n month before the last date in field mysql?

I have a question with mysql.
I have a table like records and records have a field registered_date which contains the dates. I want to select last n months but not before the current date. I must select the last date that registered_date contains.
registered_date
2015-05-30
2015-05-29
2015-05-28
2015-05-27
...
and we are in july. The last date is 2015-05-30. I want to select last 3 months before the 2015-05-30.
I tried to like these:
.... where registered_date > DATE_SUB(now(), INTERVAL 6 MONTH)
and
registered_Date between now() - interval 30 day and now()
Thank you.
You need to find the biggest date and then select three months before that:
select t.*
from table t cross join
(select max(registered_date) as maxrd from table t) m
where t.registered_date >= maxrd - interval 3 month;
Did you try like this
SELECT * FROM table WHERE registered_date >= now()-interval 3 month;

Select date with n days from the date added

I have a mysql table with the following fields
Name | Email | Date | Status
I want to extract the records where date range is between 30 days
Assume today is 2014/12/9
ie. date values are
2014/11/25
2014/12/2
2014/12/1
2014/10/25
2014/11/9
I need the o/p as (the number of days should be with in 30 days from the db date to today date)
2014/11/25
2014/12/2
2014/12/1
2014/11/9
I want to extract records those have the interval of less than 30 days from the date in the db.
Yes. I want to fetch the record between 2 days. For this I used this query
SELECT * FROM tbl_jobboard WHERE dtDate <= ( dtDate +30 )
But it is not working.
How to write the select query?
USE DATE_SUB like this:
SELECT * FROM table1
WHERE `date` BETWEEN DATE_SUB(CURDATE(),INTERVAL 30 DAY) AND CURDATE()
Working Fiddle Demo: http://sqlfiddle.com/#!2/6344f2/1
use following query
select * from table_Name t where t.date<=now() and t.date>=DATE_SUB(now(),
INTERVAL 31 DAY)

Categories