Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Mysql select problem

Posted on 2014-02-27
6
Medium Priority
?
359 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 2000 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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 111

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
The viewer will learn how to count occurrences of each item in an array.
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
Course of the Month20 days, 15 hours left to enroll

864 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