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;
Related
let me explain my purpose first, i have an vehicle booking application where, visitor will add start date and end date of his journey, in the database there is list of drivers with there availability (available_from_date and available_to_date) which is kind of duration during which they are operating, there is an field for exclude_dates for some specific dates when they are not working.
the application needs to find a list of vehicles which are available during the journey dates entered by the user.
for example user enters he want to go from place A to B during 13th sept, 2014 to 17th sept, 2014
then database needs to return a list of taxi which are available during this period and must not have any exclude date within this period.
Now i have stored the exclude_dates in comma separated format in table (i could have created a separate table but then it would take much more time for a query to execute)
I was trying to create a mysql function which would be called within the actual search query and would return true if there is some there is some excluded date present within the duration and false if not.
these are the queries that i have written
SELECT id, exclude_dates
FROM `taxi_route`
WHERE status = 1
AND `to_city` = 'Surat'
AND `from_city` = 'Ahmedabad'
AND `trip_type` = 2
AND `available_from_date` <= '2014-09-13'
AND available_to_date >= '2014-09-17'
AND STR_TO_DATE((SELECT `split`(exclude_dates, ',', 1)),'%d-%m-%Y')
NOT BETWEEN STR_TO_DATE('13-09-2014','%d-%m-%Y')
AND STR_TO_DATE('17-09-2014','%d-%m-%Y')
Split is a function i have created in mysql to separate the dates present in comma format
DELIMITER $$
CREATE FUNCTION split( str VARCHAR(500), delchar VARCHAR(2), x INT )
RETURNS VARCHAR(500)
BEGIN
RETURN SUBSTR(SUBSTRING_INDEX(str, delchar, x),
LENGTH(SUBSTRING_INDEX(str, delchar, x-1))+IF(x > 1, 2, 1));
END$$
DELIMITER ;
this works fine as far as i pass 1 in split(exclude_dates, ',', 1) , but if the exclude_dates have more then one date then this will not work
can someone please suggest or guide, how this can be accomplished.
snapshot of database is here http://i.imgur.com/JaI8MSx.png
Your query is most likely going to take more time to execute than defining a separate table for exclusion dates. It's not a good practice using comma separated list inside a column for searching purposes, this is against normalization rules.
You should define your tables separately, (e.g. taxi, taxi_route, taxi_route_exclusion, route_exclusion) and later add necessary indexes to make your searches more efficient.
Example:
taxi
---------
id
country
***
***
***
taxi_route
-------------------
id
taxi_id
available_from_date
available_to_date
from_city
to_city
route_exclusion
---------------
id
taxi_id
exclusion_date
And also add a relation table between taxi_route and route_exclusion tables to represent many-to-many relationship. Later define foreign keys on taxi_route_route_exclusion table to point taxi_route and route_exclusion tables.
taxi_route_route_exclusion
--------------------------
taxi_route_id
route_eclusion_id
Define foreign keys like:
taxi_route.taxi_id -> taxi.id
taxi_route_route_exclusion.taxi_route_id -> taxi_route.id
taxi_route_route_exclusion.route_exclusion_id -> route_exclusion.id
Define indexes like:
taxi: IX1 (status, trip_type)
taxi_route: IX1(to_city, from_city, available_from_date, available_to_date)
Your final query should look like this:
SELECT tr.id, re.exclusion_date
FROM `taxi_route` tr JOIN `taxi_route_route_exclusion` trre
ON tr.id = trre.taxi_route_id
JOIN `route_exclusion` re
ON re.id = trre.route_exclusion_id
JOIN `taxi` t
ON t.id = tr.id
WHERE
t.status = 1
AND t.trip_type = 2
AND tr.to_city = 'Surat'
AND tr.from_city = 'Ahmedabad'
AND tr.available_from_date <= '2014-09-13'
AND tr.available_to_date >= '2014-09-17'
I have the following data in php:
ID & Timestamps for each blog
Then I have a table which contains:
| id | blog | timestamp |
I need to find out which of my php timestamp data does not equal the timestamp in the mysql database.
Sure I could run a hundred queries for each and every timestamp I have in php to look if it is changed, but that seems inefficient.
Is there any way to put a single query in Mysql to get the data?
To clarify, my php ID & timestamps are in an array like
Array ( [3] => Array ( [timestamp] => 1389414084 ) )
I need to check in mysql if the record with ID=3 has the timestamp of 1389414084.
Problem is, with a lot of data to check this would end up being a huge number of queries. And I only need the ones that do not match.
I have no idea how to go about this so any help would be very appreciated.
To clarify - the first data is json. That's how it has to be. I decode it and then have to match it up to the blogs timestamp.
What I would suggest is make hash of ID, Blog & Timestamp and store it in the table itself. And instead of checking id & timestamp, you may check for the hash match (select * from my BlogTimeStamp where hash NOT IN ('hash1', 'hash2', ....).
I don't know your exact requirement. maybe there would be a better solution altogether
SELECT *
FROM myBlogTable
WHERE timestamp NOT IN (1389414084);
If you use a select statement to get the 1389414084 then insert that in the parentheses instead of hard coding in the timestamp so that the query is dynamic if you ever need it to be.
Here's what I did in the end. I just broke down the query and constructed it so that I had everything in one query.
SELECT * FROM title WHERE id IN((SELECT id FROM title WHERE id = '75' AND date ='1392501995'),(SELECT id FROM title WHERE id = '74' AND date ='1392401481'))
So you just create it in php.
$i = count($fav);
$sql = 'SELECT * FROM title WHERE id IN(';
foreach($fav as $key => $val){
$sql .= "(SELECT id FROM title WHERE id = '".$key."' AND date ='".$val["date"]."')";
if($i > 1) {
$sql .= ",";
}
$i = $i-1;
}
$sql .= ")";
I have an sql query that I use to display the news section of my website.
I would really love for the dates to be presented as "2nd January, 2012" however as I am selecting all fields from 5 tables I don't know where to put my formatting requirements (I am not selecting individual fields).
My query is below:
$query_newsheadlines = "
SELECT *
FROM
NewsArticles,
NewsArticleCategories,
NewsArticlePhotos,
NewsCategories,
NewsPhotos
WHERE
NewsArticles.id = NewsArticleCategories.newsArticleID
AND NewsArticles.id = NewsArticlePhotos.newsArticleID
AND NewsArticlePhotos.newsPhotoID = NewsPhotos.id
AND NewsArticleCategories.newsCategoryID = NewsCategories.id
AND NewsCategories.SectionID = 201
ORDER BY NewsArticles.publishDate DESC";
Any ideas would be appreciated :)
update the column my date is located in is NewsArticles.publishDate
you need to specify what column do you want to be formatted (just don't be lazy on specifying the column). Use DATE_FORMAT
SELECT DATE_FORMAT(CURDATE(),'%D %M, %Y')
SQLFiddle Demo
Other Source(s)
DATE_FORMAT()
Now currently facing a problem that is..
My table name is schedule
One of the data field is 'Depart'
the record is
row 1 = 19-08-2012 08:00:00AM,
row 2 = 19-08-2012 12:00:00PM,
row 3 = 20-08-2012 07:00:00PM,
I just want to display the date only and it is distinct
mysql_query(Select distinct depart from schedule);
This display date and the time.
Any one here know how to display the date only?
Example
SQL (recommended)
SELECT DISTINCT DATE( depart )
FROM `tbl`
PHP
$d = "19-08-2012 08:00:00AM";
echo date("d-m-Y",strtotime($d));
You can use DATE_FORMAT() function in the Mysql, as follows :
select DATE_FORMAT(distinct(Depart),'%m-%d-%Y') from schedule
Yes, use DATE:
SELECT DISTINCT DATE(`depart`) FROM `schedule`
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.