Jump to content

Archived

This topic is now archived and is closed to further replies.

jbalanski

Date Query

Recommended Posts

Hi

I have a table with 12 "expiry date: columns for different tickets. How can I do a query that would select all the expiry date columns that would include all dates less than today and also include 90 days in the future. I can't use multiple ANDS or OR's any suggestions?

The following is my query as it goes through the query as I add and statements there is no data. The first statement after where brings back 50 rows if I add the second it then drops to 10 and so on.........hopefully this makes sense.

SELECT
users.lname,
users.fname,
users.positionappfor,
users.hdsaliveexp,
users.firstaidexp,
users.fallproexp,
users.whimisexo,
users.regrescueexp,
users.pitsexp,
users.defdriveexp,
users.otherexp,
users.bopexp,
users.boilerexp,
users.confinedexp,
users.loaderforkliftexp,
users.jpickerboomerexp,
users.boplineexp,
users.tdgexp,
users.detflameexp,
users.driveexpire,
users.driversabstract,
users.drivedemerit
FROM
users
WHERE
(users.hdsaliveexp < date_sub(now(), interval -90 day)) AND
(users.firstaidexp < date_sub(now(), interval -90 day)) AND
(users.fallproexp < date_sub(now(), interval -90 day)) AND
(users.whimisexo < date_sub(now(), interval -90 day)) AND
(users.regrescueexp < date_sub(now(), interval -90 day)) AND
(users.pitsexp < date_sub(now(), interval -90 day)) AND
(users.defdriveexp < date_sub(now(), interval -90 day)) AND
(users.otherexp < date_sub(now(), interval -90 day)) AND
(users.bopexp < date_sub(now(), interval -90 day)) AND
(users.boilerexp < date_sub(now(), interval -90 day)) AND
(users.confinedexp < date_sub(now(), interval -90 day)) AND
(users.loaderforkliftexp < date_sub(now(), interval -90 day)) AND
(users.jpickerboomerexp < date_sub(now(), interval -90 day)) AND
(users.boplineexp < date_sub(now(), interval -90 day)) AND
(users.tdgexp < date_sub(now(), interval -90 day))

Share this post


Link to post
Share on other sites
That's an ugly looking query... you really should break these out into a separate table so that you can just have a single WHERE clause.  Otherwise, it's going to get even uglier, because you'll need ORs, and then find out which one was the match.

Share this post


Link to post
Share on other sites
I thought of doing seperate tables but I am just trying to do a report that selects all the users with the following expiry dates that are less then today or will expire within 90 days. If user one only has one expiry date he will show up if he has 2 expiry dates those dates and only those will show up if user 2 has 5 expiry dates I will see those and so on.

Share this post


Link to post
Share on other sites
So what? If each expiry date / item pair was a record, this would be a most trivial query.

Share this post


Link to post
Share on other sites
Maybe I am not making myself clear. (Maybe I am complicating it)

I have a form with 16 date fields these are expiry dates for various certificates.

We can do a query that will select the appropriate data ie any dates less than today and 90 days in the future.

What ends up happening as you know is you get the user with 16 colums of dates with the one or two results from the query and the rest with dates that are over 90 days.

I created a seperate table with userid expirydate and such but then adding via the form is difficult.

Is there a way to highlight the query results in php so that if I get  the user and the 16 date colums is it possible to highlight those results ie be it bold or a different color so they are easily seen.

Share this post


Link to post
Share on other sites
Why would adding via the form be "difficult"?  I think having 16 columns to query is much more difficult.

Share this post


Link to post
Share on other sites
Why not have a table of expiry_items, with a link back to user_id, that contains the item type (e.g. bop, boiler), the expiry date, and the FK?  This would make queries very, very simple, and keep your DB normalized.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.