Solved

Mysql select problem

Posted on 2014-02-27
6
343 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 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to refresh a page from a sub domain in ajax ? 34 46
PHP: Filling Out/Creating a PDF 29 102
Wordpress and Wufoo 1 39
blocking the posted data from outside of my domains 8 23
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 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