I am having column DOB which have dob of person and store it in (y-m-d) format. i want to know what will be the query to get today's birth date.
I had tried date(y-m-d) but it only show today's date result.
i.e. if there is 2018-2-20 as dob in my db then it will show in my result but if there is 1995-2-20 this date is not shown as result so basically i just want to display who's birthday is today.
You can try like this
MySQL Query :
SELECT *
FROM
YourTable
WHERE
DATE_FORMAT(BirthDayField,'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')
This query convert your desired date and current date to m(month)-d(day) format and compare
Note : Here BirthDayField is column name with data type DATE
Details about MySQL DATE_FORMAT function
first get the current date.
$today = date("yyyy-mm-dd");
then init the query where you say what should be selected.
$query = sprintf("SELECT * FROM bla WHERE `DOB` = %s", $today);
I guess you try something like this, using a case statement to determine if a record is a current birthday
select *,
case when day( date( dob ) )=day( date( now() ) ) and month( date( dob ) )=month( date( now() ) ) then 1
else 0
end as 'birthday'
from table
You can write like this
SELECT *
FROM persons
WHERE DATE_ADD(birthday,
INTERVAL YEAR(CURDATE())-YEAR(birthday)
+ IF(DAYOFYEAR(CURDATE()) >= DAYOFYEAR(birthday),1,0)
YEAR)
BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 0 DAY);
I think this will help you to getting the users who have b'day today.
Related
I have a table (Hire Rates) which contains multiple records. I'm trying to find records based on a date query but cannot get it to work correctly.
Essentially I am looking for records that contain a date between two dates. If my query date is "2021-08-15", it should return the third row (3000) as the query date falls between the two dates.
It is mostly working for me, except if the query date is equal to the start date or end date - in that case it doesn't return any result.
Table
startDate
endDate
hireRate
2021-01-01
2021-03-05
2350
2021-03-06
2021-04-08
2890
2021-04-09
2021-09-15
3000
Query
$sql = "SELECT rate, currencyID FROM hire_rates WHERE status = '1' AND NOT (startDate >= '$queryDate' OR endDate <= '$queryDate')
Try adding 'between' condition.
Like
select rate, currencyId from hire_rates where states =1 and not (cast($queryDate as DATE) between cast(startDate As DATE) and cast(endDate AS DATE) )
You would pass in the date you care about as a parameter. Then you can use between (based on your description of the logic):
SELECT rate, currencyID
FROM hire_rates
WHERE status = 1 AND
? BETWEEN startDate AND endDate;
Where ? is a parameter for the date you are passing in.
For 2021-08-15, you can write:
WHERE status = 1 AND
'2021-08-15' BETWEEN startDate AND endDate;
If you want the current date, you can actually get that from the database:
SELECT rate, currencyID
FROM hire_rates
WHERE status = 1 AND
curdate() BETWEEN startDate AND endDate;
Just try this then, `$sql = SELECT hireRate FROM hire_rate WHERE startDate<= '$queryDate' and endDate >= '$queryDate'
I want to get count of previous day records from database.
I am using following method
$date = date('Y-m-d H:i:s', strtotime('-1 day'));
$users = 'SELECT Count(*) FROM users where date="'.$date.'"';
This is show count 0 as date format in database is (Y-m-d H:i:s).
Thanks.
Could just do
select count(*) from users where to_days(date) = (to_days(now()) - 1);
This is useful if your date column is a datetime - we're just converting to a day number and checking how many records have yesterdays day number.
Hope it will help you
SELECT COUNT(*) FROM users WHERE date = (CURDATE() - INTERVAL 1 DAY)
You might want to consider asking MYSQL itself about it, so that PHP doesn't have to compute it (and it is likely to be faster) :
SELECT Count(*) FROM users WHERE date = DATE_SUB(NOW(), INTERVAL 1 DAY)
I do not understand how to get output from my sql table .
the table date is stored in int . for example date output from one row is like 1362157869 .
i want to show today orders in query :
php : $today = date("y-m-d", time());
query : SELECT * FROM test WHERE date = '$today'
but it didn't work . i also try this :
SELECT * FROM test WHERE date LIKE '$today'
Query without PHP var:
SELECT * FROM test WHERE date = DATE(NOW());
You are storing your date as a UNIXTIMESTAMP, so you have to convert it to DATETIME using FROM_UNIXTIME, and then you have to extract only the date part, ignoring the time, using the DATE() function:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
Please see fiddle here. You can then extract yesterday records with something like this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE() - INTERVAL 1 DAY
Or a specific date with this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = '2013-03-01'
To make use of an index
Assiming that your date column could contain not only the date part but also the time, you could also use this that will return all today records:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE())
and this for yesterday:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 1 DAY)
AND date < UNIX_TIMESTAMP(CURDATE())
or this for a specific date:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP('2013-03-01')
AND date < UNIX_TIMESTAMP('2013-03-01' + INTERVAL 1 DAY)
Here is a query to get all rows in the past month.
$time = time() - 9676800;
$q = $this->db->query("
select id
from ipAddress
where date > {$time}
");
But how can i adjust this query to get all rows BESIDES the past month? Basically I want to end up deleting all rows over 1 month old
So long as your methodology for calculating "last/past month" satisfies you, then it's simple:
where date <= {$time}
You can use date_format, date_sub functions to get last month's dates.
Find answers here:
mysql last month date statement
and here
MySQL Query to calculate the Previous Month
You can do it like so
$time = strtotime('-1 Month');
$q = $this->db->query("
select id
from ipAddress
where `date` <= {$time}
");
but if date is a TIMESTAMP, DATE, or DATETIME string like 2013-02-27 22:16:38 or 2013-02-27 then you need something like
$time = date('Y-m-d H:i:s', strtotime('-1 Month'));
$q = $this->db->query("
select id
from ipAddress
where `date` <= '{$time}'
");
Or purely in SQL
select id
from ipAddress
where `date` <= DATE_SUB(NOW(), INTERVAL 1 MONTH)
And if I remember correctly, date is a reserved mysql word so use backticks in your sql.
Using PHP/MySQL
I'm trying to create a select statement that gets the data from the least day of the current week (I'm using it to show data on a certain player 'this week'). The week starts on Sunday. Sundays's data may not always exist therefore if the Sunday data isn't found then it would use the next earliest day found, Monday, Tuesday, etc.
My date column is named 'theDate' and the datatype is 'DATE'
The query would need to be something like:
SELECT *
FROM table_name
WHERE name = '$username'
AND [...theDate = earliest day of data found for the current week week]
LIMIT 1
It would return a single row of data.
This is a query I tried for getting the 'this week' data, It doesn't seem to work correctly on Sunday's it shows nothing:
SELECT *
FROM table_name
WHERE playerName = '$username'
AND YEARWEEK(theDate) = YEARWEEK(CURRENT_DATE)
ORDER BY theDate;
This is the query that I'm using to get 'this months' data and it works even if the first day of the months data is not found, it will use the earliest date of data found in the current month/year (this query works perfect for me):
SELECT *
FROM table_name
WHERE playerName = '$username'
AND theDate >= CAST( DATE_FORMAT( NOW(),'%Y-%m-01') AS DATE)
ORDER BY theDate
LIMIT 1
Without trying this, you probably need an inner query:
select *
from table_name tn
where tn.the_date =
(select min(the_date)
from table_name
where WEEKOFYEAR(the_date) = WEEKOFYEAR(CURDATE())
and YEAR(the_date) = YEAR(CURDATE()))
viz, give me the row(s) in the table with a date equal to the earliest date in the table in the current week and year.
Try this
SELECT * FROM table_name WHERE name = '$username'
AND your_data IS NOT NULL
AND WEEK(the_date,0 = WEEK(NOW(),0))
ORDER BY DATE_FORMAT(the_date,'%w') ASC
Try the following, replace YOUR_DATE with the date from the column you want (theDate):
SELECT ADDDATE(YOUR_DATE, INTERVAL 1-DAYOFWEEK(YOUR_DATE) DAY)
FirstDay from dual
Did you try:
SELECT ADDDATE(theDate , INTERVAL 1-DAYOFWEEK(theDate ) DAY) FirstDay
FROM table_name
WHERE playerName = '$username'
ORDER BY theDate DESC
LIMIT 1