What should be MySQL query for the following? - php

I wish to show upcoming birthday records on dashboard. I have MySQL table 'contacts' which
has field dob (DATE YYYY-MM-DD). I want to retrieve all the records with birthday is coming
in next 15 days. e.g is: birthday is 1986-03-24 it should be in result.

Take a look at MySQL DATE_ADD and INTERVAL.
By using both as below you can get record that are coming in next 15 days.
SELECT * FROM user WHERE dob BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 15 DAY)
FOR birthrate comparison.
MAKEDATE(YEAR(NOW() + INTERVAL 15 DAY), DAYOFYEAR(birthday)) BETWEEN CURDATE() AND CURDATE() + INTERVAL 15 DAY
SQLFiddle Demo

You have to use the date in mysql query as:
SELECT FROM table_name WHERE timestamp_row BETWEEN
'2011-01-01 00:00:00' AND '2011-01-02 23:59:59';
You are doing the B'Day dates of users and thus hope this would be do the trick of formatting and selection of date
select id from my_table
where
timestamp < date_format(date_add(CURRENT_TIMESTAMP(), interval 1 day),
'%Y%m%d000000' ) AND timestamp >= date_format(CURRENT_TIMESTAMP(),
'%Y%m%d000000')
Can do by this way also
SELECT * FROM person WHERE IF ( MONTH( NOW() ) < 12,
MONTH( birthdate ) = MONTH( NOW() ) + 1, MONTH( birthdate ) = 1)

You can use this query. Try Out
SELECT * FROM TableName
WHERE REPLACE(BirthDate, DATEPART(YEAR, BirthDate), DATEPART(YEAR, GETDATE())) BETWEEN GETDATE() AND DATEADD(DAY, 15, GETDATE())

Related

how to take the count based on first week and second and third week and fourth week

I have one table name (task), here i want take the count like how many registration happend in first and how many registration happend in second how how can do this , i am the new persion of mysql and PHP .
id name t_created_on
1 Kani 2017-03-03 12:45:18
2 yuvi 2017-03-04 12:45:18
3 Mahesh 2017-03-11 12:45:18
Here i am using this format date("Y-m-d h:i:s")
Bassed on my database first week 2 registration and second week is 1.
Expected results:
First Week count : 2
Second week count : 1
1.
SELECT count(*) as records FROM tbl
WHERE t_created_on >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND t_created_on < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
2.
SELECT count(*) as weekrecord FROM tb1
WHERE
WEEK (date) = WEEK( current_date ) -1 AND YEAR( date) = YEAR( current_date );
note: WEEK( current_date ) -1: number indicates number of week.
SELECT WEEK(t_created_on) as week, count(*) as count
FROM my_table
GROUP BY WEEK(t_created_on)
ORDER BY WEEK(t_created_on);
SELECT count(*) as weekrecord, MONTH(current_date) as monthwise
FROM tb1
WHERE
WEEK (date) = WEEK( current_date ) -1
AND YEAR( date) = YEAR( current_date );

sql order by most recent dates

I have this table in my database:
INSERT INTO `shop_stats` (`date`, `value`) VALUES
('09/2014', 326),
('08/2014', 1007),
('07/2014', 1108),
('06/2014', 1027),
('05/2014', 895),
('04/2014', 650),
('03/2014', 683),
('02/2014', 563),
('01/2014', 499),
('12/2013', 568),
('11/2013', 522),
('10/2013', 371),
('09/2013', 347),
('08/2013', 376),
('07/2013', 418),
('06/2013', 567),
('05/2013', 357);
i need to find a way to display the last 12 months.
I tried this:
SELECT * FROM shop_stats ORDER BY date DESC LIMIT 12
But it doesn't work correctly.
Any suggestions ?
SELECT * FROM shop_stats WHERE date >= DATE_SUB(NOW(),INTERVAL 1 YEAR) LIMIT 12
Your "dates" are stored as strings, presumably with the month first. So, the following order by should work
order by right(date, 4), left(date, 2)
You need to put the year before the month for ordering purposes.
If you want the last twelve months, I would recommend:
where right(date, 4) * 12 + left(date, 2) >= year(now()) * 12 + month(now())
order by right(date, 4), left(date, 2)
The where statement converts the dates to a number of months, for both the "date" column in your data and for the current time.
You can simply use STR_TO_DATE like this
SELECT
*
FROM
shop_stats
order by
STR_TO_DATE(date, '%m/%Y') DESC LIMIT 12
Demo
I suppose your field date has a type string
So you try this:
SELECT * FROM shop_stats
ORDER BY SUBSTRING(date, 4, 4) desc,
substring(date, 1, 2) DESC LIMIT 12
Show Sql Fiddle
You can take a look at DATE_SUB
SELECT * FROM shop_stats where date >= DATE_SUB(now(), INTERVAL 12 MONTH) ORDER BY date
Edit:
How about converting the string to date & doing the appropriate date operations in the sql ?
SELECT DATE
,t1.value
FROM (
SELECT DATE
,STR_TO_DATE(CONCAT (
'01/'
,DATE
), '%d/%m/%Y') date_
,value
FROM shop_stats
) t1
WHERE t1.date_ >= DATE_SUB(now(), INTERVAL 12 MONTH)
ORDER BY t1.date_ DESC
http://sqlfiddle.com/#!2/2be05/8
select * from shop_stats where date >= (NOW() - INTERVAL 12 MONTH) ORDER BY date

Mysql Query back to midnight only

I have this query that looks up results from a database for the last twenty minutes, now i know how to look up in hours, days, etc, but is it possible to look up only as far back as midnight of that day. so when ever the query is run and what ever time it only looks back as far as midnight?
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) and ip='$ip'
This is my code but is there away in which i can replace the interval for a specific time.
Any help would be appreciated.
Looking back to midnight of the current day is the same as looking at the current date with no time component. You can therefore use DATE() to truncate the datetime column date to only the date portion, and compare it to CURDATE().
SELECT * FROM ip_stats WHERE DATE(date) = CURDATE() and ip='$ip'
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) AND date >= CURDATE() and ip='$ip'
Just make the column date be bigger than the curdate (which is the starting of this day).
SELECT * FROM ip_stats
WHERE date >= ( NOW() - INTERVAL 20 MINUTE )
and date > CURDATE()
and ip='$ip'
You can use CURDATE() to get rows since midnight of the current day:
SELECT * FROM ip_stats WHERE date >= CURDATE() and ip='$ip'
SELECT *
FROM ip_stats
WHERE date >= GREATEST( NOW() - INTERVAL 20 MINUTE, CURRENT_DATE )
AND ip = '$ip'

HOw to SELECT * (all) which date start from the last day of last month to the first day of next month

use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.

How would I get the birthdays of friends who are celebrating their birthday this week, this month and next month using MYSQL and PHP?

I am currently working in an site which needs to get the birthdays of friends who are celebrating their birthday this week, this month and next month using MYSQL and PHP.
How would I go about this?
Provided you store birthdates in DATE (or DATETIME) format in MySQL you can use the following Queries:
// This week
SELECT * FROM person WHERE WEEK( birthdate ) = WEEK( NOW() )
// This month
SELECT * FROM person WHERE MONTH( birthdate ) = MONTH( NOW() )
// Next month
SELECT * FROM person WHERE MONTH( birthdate ) = MONTH( NOW() ) + 1;
SELECT * FROM person WHERE IF
( MONTH( NOW() ) < 12, MONTH( birthdate ) = MONTH( NOW() ) + 1,
MONTH( birthdate ) = 1)
SELECT *
FROM users
WHERE birthday + INTERVAL YEAR(CURRENT_DATE) - YEAR(birthday) YEAR BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL 3 DAY
Note that this condition is not sargable and requires full indes scan.
In SQL Server, Oracle and PostgreSQL this can be improved using this approach:
Selecting birthdays
, but MySQL lacks a way to generate a random resultset.
However, you can create a dummy table and store years from 1900 to 2100 in it.
Add a cron task to update it in 2100 since you can forget easily :)

Categories