Jump to content


Photo

Date Query


  • Please log in to reply
7 replies to this topic

#1 jbalanski

jbalanski
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 10 October 2006 - 03:49 PM

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))

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 October 2006 - 03:59 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 jbalanski

jbalanski
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 10 October 2006 - 04:34 PM

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.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 October 2006 - 05:00 PM

So what? If each expiry date / item pair was a record, this would be a most trivial query.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 jbalanski

jbalanski
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 02:45 PM

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.

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 12 October 2006 - 03:32 PM

Why would adding via the form be "difficult"?  I think having 16 columns to query is much more difficult.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 jbalanski

jbalanski
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 October 2006 - 06:44 PM

Could you explain further

#8 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 12 October 2006 - 07:55 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users