?
Solved

MySQL Query by date range

Posted on 2011-03-08
19
Medium Priority
?
514 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 41

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 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