Solved

Mysql select problem

Posted on 2014-02-27
6
335 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
  • 3
  • 2
6 Comments
 
LVL 34

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 34

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

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 108

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 34

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to count occurrences of each item in an array.
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.

910 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

19 Experts available now in Live!

Get 1:1 Help Now