Author Topic: [SOLVED] Several WHERE statements not returning data  (Read 760 times)

0 Members and 1 Guest are viewing this topic.

Offline drranchTopic starter

  • Enthusiast
  • Posts: 78
    • View Profile
[SOLVED] Several WHERE statements not returning data
« on: November 23, 2007, 02:08:01 AM »
I have a WHERE statement that pulls data from 4 different tables based on a date range... When I removed the "vc.follow_up_date I get information for the other follow_up_dates, but when I leave vc.follow_up_date in the query nothing is returned.  The vc.follow_up_date doesn't have any data in its table for this date range either.  When I put in data for this date range I get back the information.  Does any one know why this is?

Code: [Select]
WHERE hc.follow_up_date
BETWEEN DATE_SUB(CURDATE() , INTERVAL 7 DAY)
AND DATE_ADD(CURDATE() , INTERVAL 7 DAY)
AND hm.follow_up_date
BETWEEN DATE_SUB(CURDATE() , INTERVAL 7 DAY)
AND DATE_ADD(CURDATE() , INTERVAL 7 DAY)
AND ip.follow_up_date
BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY)
AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)
AND vc.follow_up_date
BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY)
AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)";

Offline ManOnScooter

  • Irregular
  • Posts: 32
    • View Profile
Re: Several WHERE statements not returning data
« Reply #1 on: November 23, 2007, 08:43:30 AM »
I suggest if u mail the table structure-it would be easier to know wots wrong where

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: Several WHERE statements not returning data
« Reply #2 on: November 23, 2007, 09:42:09 AM »
I suggest if u mail the table structure-it would be easier to know wots wrong where
We don't *mail* things on a forum, we post them in the original thread!

As for the OP, I would guess it a precendence issue... try this:

Code: [Select]
WHERE
( hc.follow_up_date BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() + INTERVAL 7 DAY )
AND ( hm.follow_up_date BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() + INTERVAL 7 DAY )
AND ( ip.follow_up_date BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() + INTERVAL 7 DAY )
AND ( vc.follow_up_date BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() + INTERVAL 7 DAY )

Of course, you should be using a sql string, not curdate() so that the query cache is utilized.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline drranchTopic starter

  • Enthusiast
  • Posts: 78
    • View Profile
Re: Several WHERE statements not returning data
« Reply #3 on: November 23, 2007, 03:39:36 PM »
Hello Fenway,

When I use your query structure I get the same results that I got with my query structure.  I added each curdate() for each item one at a time and I get back information for each step but when I add the vc.follw_up_date I get blank; no information.  I know that the table/column vc.follow_up_date is blank and since its blank the query sends nothing back even though there is data for the other follow_up_dates.

I tried using "OR" instead of "AND" but I get back all follow-updates on all tables regardless of the "BETWEEN" structure.
« Last Edit: November 23, 2007, 03:40:43 PM by drranch »

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: Several WHERE statements not returning data
« Reply #4 on: November 23, 2007, 03:47:38 PM »
What do you mean "is blank"? Like there's no row? Then you need a left join...
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline drranchTopic starter

  • Enthusiast
  • Posts: 78
    • View Profile
Re: Several WHERE statements not returning data
« Reply #5 on: November 25, 2007, 12:40:41 AM »
I don't think I'm on the right track...

I tried using the join statment, but it doesn't work. I think my table configuration is preventing me from using a single MYSQL query, so I broke down each of these queries and used a != if php statement to display when there are no appointments.

Thank you all for your assistance.

hc table
hcusername
hmnickname
hmfollow_up_date

hm table
hmusername
hmnickname
hmfollow_up_date
---------------
ip table
ipusername
ipnickname
ipfollow_up_date
----------------
vc table
vcusername
vcpnickname
vcfollow_up_date

All tables have data in their fields except the vc table where there is no data within the date range the mysql query is searching for.  When I leave the vc table in the query no data is returned, but the ip and hm tables do have data.  When I remove the vc table from the mysql query the data from hc table, hm table, and ip table is returned.  I'd like to see all data returned even when there isn't any data in either one of the tables for the queried date. 

*Solution seperated the query to individual queries and then displayed results on web page using != if php statment to state when there is no result show "there are no current appointments at this time" and if there are current appointments show the appointments from which ever table has an appointment for that time frame.
« Last Edit: November 25, 2007, 12:53:00 AM by drranch »

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: [SOLVED] Several WHERE statements not returning data
« Reply #6 on: November 26, 2007, 07:54:25 AM »
Well, I'm not sure why you're using AND... how are these tables related?
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline drranchTopic starter

  • Enthusiast
  • Posts: 78
    • View Profile
Re: [SOLVED] Several WHERE statements not returning data
« Reply #7 on: November 26, 2007, 10:28:46 PM »
Each table represents a certain "type" of appointment, hence different tables.  On the users home page of the signed on user I want to display any appointment they may have on any of these four tables that is with in 7 days out from the current date.  So there may be times that they have an appointment for only one of these four tables or for all tables or multiple appointments for one of these tables.

Originally I was hoping for a single MYSQL query of all tables and then build a single PHP script to display it on the users home page.


Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
Re: [SOLVED] Several WHERE statements not returning data
« Reply #8 on: November 27, 2007, 11:49:53 AM »
Then you want to UNION each inidividual select.
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.