Solved

MySQL Query by date range

Posted on 2011-03-08
19
504 Views
Last Modified: 2012-05-11
The bulk of my new portal is to query data based on users submissions by date range.  I have all the information in 2 tables in my database.  a table for the church information and another table for the weekly submitted information.  The table setup is:

tblChurch
churchID
churchName
etc.

tblChurchInfo
churchInfoID
weeklyGiving
date
churchID

i can figure out how to write mySQL query to get all the data input by church, and church name, what i'd like is to add a filter where the data could be filtered by date.  any suggestions?
0
Comment
Question by:axessJosh
  • 9
  • 9
19 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35074022
Hi again axessJosh,

How exactly is the workflow going to happen?  The users will submit a date or a date range or do you just need to know how to construct the query?
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35074122
both.

at the user level, they will login and submit their data and select a date (i.e. this past sunday) for the data.

then;

at the admin level, i need for that data to be queried by the date selected by the admin.  I'd like for them to be able select date ranges and see data trends and changes.  For right now, if i can just get the data queried by date i'd be happy.

make sense?
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 250 total points
ID: 35074356
You need to set an advanced recordset and then write the where clause using the field submitted from the form.  The SQL would look something like this:

SELECT tblChurch.*, tblChurchInfo.* FROM tblChurch INNER JOIN tblChurchInfo ON tblChurch.churchID = tblChurchInfo.churchID WHERE tblChurchInfo.date Between YYYY-MM-DD And YYYY-MM-DD;

The above would select a range of dates if you are using two date fields.  In the Advanced View, you would set these as variables using $_POST['datefieldinformname1'] and $_POST['datefieldinformname2'] as the run-time variables.  To do a single date, the SQL would be:

SELECT tblChurch.*, tblChurchInfo.* FROM tblChurch INNER JOIN tblChurchInfo ON tblChurch.churchID = tblChurchInfo.churchID WHERE tblChurchInfo.date = YYYY-MM-DD;
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35074375
Are you looking for something like this?
select *
  from tblChurch c
  join tblChurchInfo ci on c.churchID = ci.churchID
 where ci.date between date1 and date2

Open in new window

0
 
LVL 2

Author Comment

by:axessJosh
ID: 35074383
In both examples would i change yyyy-mm-dd to the form variables or leave it as is?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35074457
Change it to the form values always. The other stuff is just placeholder data
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100501
OK, here is what i landed on that works for my purposes.

SELECT tblchurch.churchID, tblchurch.churchName, tblchurchinfo.churchAttendance, tblchurchinfo.churchSSAttendance, tblchurchinfo.churchResponsibility, tblchurchinfo.dateID, tblchurchinfo.churchID FROM tblchurch JOIN tblchurchinfo ON tblchurch.churchID = tblchurchinfo.churchID WHERE tblchurchinfo.dateID = dateVar

dateVar is a dreamweaver variable i made up and is using $_POST['dateID'] to capture the user's input.

now i need have populated a drop down menu with the potential dates to select using DISTINCT in my SQL statement.

how can i pass that variable to my original SQL statement to retrieve the results by dateVar?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100556
Just have the form field use dateID as the field name and on post, it will submit $_POST['dateID']
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100590
it is sending the variable data to the URL, how can i pass just to the page?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100603
<form method="post">

You have to choose...post or get.  Can't have it both ways in the same form.  You might have to use two different forms...
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100621
should i have anything for <form action="" >
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100638
You don't have to set an action.  Forms will post to themselves in absence of an action parameter.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100647
when I select a field, it tries to send me to a new page.  this is the error code i get as a 404 not found.

http://localhost/district_contentDB/2011-02-27

how can i have the jump menu not send to another page?
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100693
Without seeing your code I have no idea, but jump menus by definition try to send the user to the selected value.  So if you built a dynamic jump menu, it's doing what you told it to do.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100722
I have attached the code below.  Currently, i am using a list to populated from a recordset using distinct to pull the date.  I then want that date to be passed and retrieved from a second recordset as the filter to display.

i added a php echo field just to check what data was being passed.  Currently, it shows the same date no matter what.
weeklySummary.php
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35100829
Try putting action="" back in the form tag and see if that fixes it.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 35100873
Tried that.

for some reason it doesn't seem like the form variable is being sent.  I am wondering if it is a problem with the dreamweaver variable code in one recordset

$dateVar_rsChurchInfo = "-1";
if (isset($_POST['dateID'])) {
  $dateVar_rsChurchInfo = $_POST['dateID'];
}

and the form is pulling the data from a different recordset?  

seems as if the data I am trying to retrieve is not making a connection that dateID is present.  (i realize that is laymans terms)
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 35101097
It's not recognizing because your recordset code (that I'm looking at from the download) is wrong:

$dateVar_rsChurchInfo = "-1";
if (isset($_GET['dateID'])) {
  $dateVar_rsChurchInfo = $_GET['dateID'];
}

0
 
LVL 2

Author Comment

by:axessJosh
ID: 35109007
I tried changing to both variations (GET and POST) and still get the same issue.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I still run into .cgi files every now and then. In some instances, I actually prefer the simplicity of a .cgi script to other options. Since I use DreamWeaver extensively, what I needed was a way to open .cgi scripts in Dreamweaver. And I wanted to …
This article is very specific and is only intended to help if you are installing Dreamweaver 8 in a Windows 7 environment with Office 2007 installed.   I'm not sure why Microsoft tends to release OS' that should not be released but they do.  Windows…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now