Solved

Mysql select problem

Posted on 2014-02-27
6
345 Views
Last Modified: 2014-02-27
The purpose of the query is to be able to run reports to see when to bill customers. When the customer file is created, they can break up their initial fee into 4 payments; however, some pay in full, some in 2 payments, etc.

The table is like this:
payment1_amount | payment1_duedate | payment2_amount | payment2_duedate | etc.
          150.00                  02/02/2014                  175.00                       02/27/2014

I tried running:
SELECT lead_id,COALESCE(payment1_amount, payment2_amount) B FROM table WHERE payment1_duedate = '$today' OR payment2_duedate =  '$today' 

Open in new window


This will display the correct date (if it's today) but it only selects the first payment. So today's payment is on the payment2_duedate/payment2_amount - it displays as payment2_duedate/payment1_amount.

I tried playing with unions and joins, but just can't get it sorted out - and Google is failing me right now :) Thanks!
0
Comment
Question by:marketizeit
[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
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39893287
That's about right.
You're asking Mysql to give you the first non-NULL value from payment1_amount, payment2_amount.
Since, regardless of what OR condition is fulfilled, payment1_amount is not null, MySQL will oblige and return this value.

HTH,
Dan
0
 
LVL 1

Author Comment

by:marketizeit
ID: 39893298
Do you have any ideas on how to display the payment amount that corresponds to today's date?
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39893307
Try this:
(SELECT lead_id, payment1_amount, FROM table 
WHERE payment1_duedate = '$today')
UNION
(SELECT lead_id, payment2_amount, FROM table 
WHERE payment2_duedate = '$today')
UNION
(SELECT lead_id, payment3_amount, FROM table 
WHERE payment3_duedate = '$today')
UNION
(SELECT lead_id, payment4_amount, FROM table 
WHERE payment4_duedate = '$today')

Open in new window

0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39893341
You might want to make a Google search for the exact phrase, "Should I Normalize My Database" and read the thoughtful writings on both sides of that concept.  I think if this were my app, I would have a separate table of payment required and payments made.  It would give you a little more flexibility and would make queries like this easier to structure.  No big deal, just a thought... ~Ray
0
 
LVL 1

Author Closing Comment

by:marketizeit
ID: 39893344
Thank you so much and esp for being so quick!!!
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39893376
Glad I could help!

And, as Ray said, when you have the time consider normalizing the payments and see if the benefits outweigh the time needed to make the changes in your application.
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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