I have a MySQL DB with a table (USERS) that stores a lot of user information including the following columns:
dob_day, dob_month, dob_year
I need to get the age of the user (exact age, not just by using the year).
This query will be used in conjunction with a search box with two drop-downs where the user selects the low_are and high_age and compares it to the age calculated above.
(users_age >= $low_age AND users_age <= $high_age)
Any help with this would be great.
Thanks.
Put the birthday in a dob_date, and the following select should work.
SELECT year(curdate()) - year(dob_date)
+ if((month(curdate() > month(dob_date))
or (month(curdate()) = month(dob_date)
and day(curdate()) >= day(dob_date)),1,0) as age from ...
Please don't kill me if I forgot a '(' or ')', but this should do the trick.
You can do a string comparison of dates:
SELECT * FROM your_table
WHERE DATE_FORMAT(CONCAT_WS('-', dob_year, dob_month, dob_day), '%Y-%m-%d') >= '1950-01-01'
AND DATE_FORMAT(CONCAT_WS('-', dob_year, dob_month, dob_day), '%Y-%m-%d') <= '2005-12-31'
If you want to keep the structure as you have specified you could use the following code query to select these users:
SELECT (YEAR(CURDATE())-YEAR(a.dob) - (RIGHT(CURDATE(),5)<RIGHT(a.dob,5))) as 'age' FROM (SELECT STR_TO_DATE(CONCAT(dob_day,',',dob_month,',',dob_year), '%d,%m,%Y') AS 'dob' FROM users) a WHERE (YEAR(CURDATE())-YEAR(a.dob) - (RIGHT(CURDATE(),5)<RIGHT(a.dob,5))) BETWEEN $low_age AND $high_age
Please see here for details about the calculation used to work out a user's age.
You can easily modify the query above to extract other user fields from the table.
Related
I am trying to get the renewal_date column and get the month to check is that the exact month I want, and if so display the details of that record. My query is:
select domain_name, renewal_date
from hosting_details
where substr(renewal_date,-5,-3) = '09'
This doesn't give any result, but when I run below query I can retrieve results:
select domain_name, renewal_date
from hosting_details
where renewal_date = '2015-09-03'
I don't want to hard code the date, I want to retrieve any details which is in 09th month. How can I do it?
You should not perform a function on your (primary) WHERE statement. It will not use the index and perform a full table scan.
SELECT domain_name,renewal_date FROM hosting_details
WHERE renewal_date >= '01-09-2015' AND renewal_date < '01-10-2015'
You can try as below :
select domain_name,renewal_date from hosting_details where
date_format(renewal_date, '%m' ) = '09'
is their any query to pick certain date between two time stamps?The Problem is these two times stamps are set in two diffrent fields
SELECT * FROM table WHERE fs_from > theDate AND fs_to < theDate;
I suggest to use prepared statements and insert a date object into the query.
In my opinion you can try in this way (If I really understood your question)
SELECT myFields FROM myTable
WHERE myCertainDate BETWEEN fs_from AND fs_to
Try this:
SELECT *
FROM table
WHERE fs_from > 'some date'
AND fs_to < 'some other date'
Which date do you want? You can choose a random date by doing:
select t.*,
date_add(t.fs_from, interval cast(datediff(fs_to, fs_from) * rand() as int) day
) as ArbitraryDateBetweenTwoDates
from table t
This is my query to search the data base for candidates who meet a certain criteria. I am using php, mysql, html
$Odata = mysql_query("SELECT * FROM p_candidate WHERE(`gender` LIKE '%".$Gender."%')
AND (`verbal` LIKE '%".$Verbal."%') AND(`waiver` LIKE '%".$Waiver."%')
AND(`waiver_type` LIKE '%".$W_Type."%') AND(`sel_staff` LIKE '%".$A_Staff."%')
AND(`sel_peers` LIKE '%".$A_Peers."%')AND(`verbal` LIKE '%".$Awake."%')
AND(`ambulatory` LIKE '%".$Ambulatory."%') AND(`function` LIKE '%".$Function."%')"
) or die(mysql_error());
There is another criteria I want to add - Adult/Child.
I have date of birth as a column in the DB. If the candidate is above 18, would fall under Adult, otherwise Child.
The user may want to search for an adult with all the contents in $Odata. How can I do this?
looking through Calculate Age in MySQL (InnoDb)
and Search age range in mysql, php I understand it can be done independently, but how can I incorporate it into my above query. Is that possible?
This might help
sample data
create table dob
(
dob datetime
)
;
insert into dob select '2001-10-08' ;
insert into dob select '1976-11-28' ;
Figure out child or adult:
select
dob,
case when dob > Current_date() - INTERVAL 18 YEAR then 'child' else 'adult' end
from dob
see http://sqlfiddle.com/#!2/2df6d/12
You can find a correct answer in the official MySQL documentation:
SELECT CASE WHEN TIMESTAMPDIFF(YEAR,dob,CURDATE()) >=18 THEN 'adult' ELSE 'child' END
FROM p_candidate;
In my code, I am trying to find items in an activities table that are within the last day. This query is not returning any results, are there any problems with it? Is there a better query?
$curday = time() - (24*3600);
$query = "SELECT * FROM activities WHERE userid = '$userid' AND 'timestamp' > '$curday'";
There are two choices here, you can get and format the date through PHP or use SQL language to do it. I prefer to do it within the SQL, it also allows me to use the same query in a MySQL client.
This question is essentially the same thing: MySQL SELECT last few days?
This would be the new query:
$query = "SELECT * FROM activities WHERE userid = '$userid' AND 'timestamp' > DATE_ADD(CURDATE(), INTERVAL -1 DAY)";
you can try with unix function 'mktime' to get value of yesterday ..
as
$curday = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
for reference
if your database will mysql only then you can extract yesterday in sql itself..
SELECT * FROM activities
WHERE userid = '$userid'
AND timestamp > DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY)
one more thing if timestamp is your column name don't put this column inside single quote ..
What you can use is DATE_SUB. This can be used as follows
SELECT * FROM activities
WHERE userid = '$userid'
AND timestamp > date_sub(current_date, interval 1 day)
This way you don't need to work with current date in PHP
in Informix it would be (TODAY - 1) if the column is type DATE
Right now I have a MySQL table with one column called "date_time" that simply lists different DATETIME values (e.g. "2010-11-30 12:55:00"). Is it possible for me to construct a query that directly:
Selects DATETIME values where the month is the current month?
Selects DATETIME values where the days aren't passed days (e.g. tomorrow and until the end of the month?
An abstract query in my mind would look like:
$query = "SELECT * FROM meetings WHERE uid = " . $_SESSION['uid'] . "AND the DATETIME month is active month AND the DATETIME day is bigger than the active day"
Is this functionality possible in MySQL (did some searching but didn't find anything helpful), or will I have to use PHP functions later to filter this stuff out? Many thanks in advance.
MySQL has a ton of date formatting functions that would solve this for you:
Select by specific month...
SELECT ... WHERE MONTH(date_time) = MONTH(now());
Select by non-passed days:
SELECT ... WHERE DAY(date_time) > DAY(now());
Of course, these sorts of queries will pick out the same month/days in ANY records from ANY years. If you want to restrict to particular chunks of time, then you'll have to add more clauses.
Something like:
$query = "SELECT * FROM meetings WHERE uid = " . $_SESSION['uid'] . "WHERE EXTRACT(month from now()) = extract(month from date_time) AND EXTRACT(day from now()) < extract(day from date_time)