Solved

Mysql select problem

Posted on 2014-02-27
6
333 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress Query 1 34
MySQL ERROR 1045 (28000) 2 40
regex expression 9 23
uploading image to a profile page 1 5
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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 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

18 Experts available now in Live!

Get 1:1 Help Now