php - MySQL JOIN and DATE comparison/manipulation
Consider the following MySQL tables:
APPLICATIONS (contains all applications by all users)
unique_id | user_id | date_of_application | date_ended | status
*ITEM_TRANSACTIONS* (contains all item transactions by all users)
{-code-2}
ITEMS
{-code-3}
So far I have the following query that already takes the the sum of the values of the item prices distinct per user.
However, what I need in addition to this is that SELECT should only cover (compute for the sum) of those item transactions that its user's status has Ended and that application being the latest one.
To reiterate, the applications and item_transactions tables will contain several records by the same user over time, what should be covered (SELECTED) are those transactions under the latest application (status Ended) only.
{-code-4}
Answer
Answer
Answer
Answer
Solution:
To select only the last ended application for each user, you can use a subquery to select the IDs of those applications and join the applications table with that subquery. Assuming that application IDs are ordered chronologically:
However, I couldn't figure out from your description exactly how the transactions should be joined to the applications. I assume you probably want something like this:
but I'm not sure about the exact join criteria. (For example, what if the item was requested during the application period but delivered after its end? Or requested before but delivered during the application period?)
Answer
Solution:
you can try this.
If you are looking for only one latest record then you need to use LIMIT 0,1 otherwise its working without that..i think this would be helpful to you.
Thanks.
Answer
Solution:
how about if you try this one:
Since you need the latest application you should get the latest date of all the ended applications no matter when it did ended, but I'm having doubt on this statement "that its user's status has Ended and that application being the latest one." cause I was thinking you might be saying that once a transaction has ended that will turn the transaction as the latest one no matter when it did started being there on the record if that is the case instead of using the date_of_application attribute you may somehow use date_ended instead.
I hope this helps.
:)