I have a table profiles. This table has afield d.o.b. (date of birth). I don’t have a field age (since I don’t have an idea how to update that field yearly), but I still would like to be able to search on age. Is this possible within a query or should I use my server side scripting to do so?
Personally, I would use server-side scripting to calculate the age in real time. OR, if you are running something that can do scheduled tasks (like ColdFusion), you can create a script that will run every night just after midnight, find all dob’s of that morning, and update an age column in the database.
I decided to still do it in mySql and found the following sollution doing the trick:
SELECT *,
EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`dob`))))+0
FROM
profiles
WHERE
EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`dob`))))+0 >= <cfqueryparam value="FORM.age_from">
That is basically working fine. The only problem I have is that when I add the date_to in the where clause i.e.
AND EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`dob`))))+0 <= <cfqueryparam value="FORM.age_to">
I don’t get any data returned. What should I adjust to make this work? Thank you in advance :tup:
coldfusion rocks big time, been using it on my personal web sites since 1997
SELECT * -- perfectly okay in an outer query
FROM ( SELECT something
, anything
, just_not_the_dreaded_evil_select_star
, EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`dob`)))) AS age
FROM profiles ) AS inner
WHERE age BETWEEN <cfqueryparam value="FORM.age_from">
AND <cfqueryparam value="FORM.age_to">
Hi Rudy. Thank you for the reply. CF indeed rocks.
I get an error using the above query:
<cfset FORM.age_from = "20">
<cfset FORM.FORM.age_to = "40">
<cfquery name="getProfiles" datasource="Db_dating">
SELECT *
FROM ( SELECT naam
, EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`dob`)))) AS age
FROM profielen ) AS inner
WHERE age BETWEEN <cfqueryparam value="FORM.age_from">
AND <cfqueryparam value="FORM.age_to">
</cfquery>
Hi Rudy. Thanks again for the reply. I have changed that and ideed don’t get an error any longer, but I also don’t get any rows returned. As soon as I have:
WHERE age >= <cfqueryparam value=“FORM.age_from”> It is working but somehow using BETWEEN it doesn’t give me any results? What can be wrong. Could it be the dob field, which is currently timestamp
timestamp is somewhat inappropriate (i mean, how many birthdates actually include the time???) but it actually works fine
if you put the WHERE clause with BETWEEN back into the query and hardcode the from and to ages (instead of using cf params) you will see there’s nothing wrong with the sql
Hi Rudy. Yes you are right. When I hard code those two it shows me the appropriate output. But I should be able to do it with form values as well! Or is it because I have used cfset on the test page?
Edit: got it working using cfparam instead of cfset and oops I had forgotten something very important Form.age_to should have been #Form.age_to# ofcource. Again thank you for all your input :tup:
Hi Rudy. I have another question about this. As you probably have noticed was the above just a test. The real query exists of many more fields and includes a few INNER JOINS like this:
SELECT P.profiel_id,
P.gebruikersnaam,
P.dob,
P.woonplaats,
PR.provincie,
G.geslacht
FROM
profielen P
INNER
JOIN provincies PR ON P.provincie_id = PR.provincie_id
INNER
JOIN geslacht G ON P.geslacht_id = G.geslacht_id
How should the query with sub query look like, In other words where should I include the INNER JOINS in the exampla as before and below:
SELECT *
FROM ( SELECT P.profiel_id,
P.gebruikersnaam,
P.dob,
P.woonplaats,
PR.provincie,
G.geslacht,
EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`dob`)))) AS age
FROM profielen ) AS sub
SELECT *
FROM ( SELECT P.profiel_id,
P.gebruikersnaam,
P.dob,
P.woonplaats,
P.geslacht_id,
P.isActive,
PR.provincie,
G.geslacht,
EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(),`dob`)))) AS age
FROM
profielen P
INNER JOIN provincies PR ON P.provincie_id = PR.provincie_id
INNER JOIN geslacht G ON P.geslacht_id = G.geslacht_id
) AS sub