[thelist] SQL Query
Jack Timmons
jorachim at gmail.com
Tue Jun 8 11:17:26 CDT 2010
I think I'm having one of those days.
I'm trying to do a query to set up some data for an email. I'm doing
this in SQL instead of PHP because I can't trust others down the road
to replicate the PHP results exactly, but I can ensure the query
always does.
I have a table of customer charges. I want to find only the latest
paid_on for each customer. So, I did a query to order the results by
when it was paid_on descending, and grouped by ID.
Well, as you can imagine, no matter what I order in, I always get the
most recent charge.
Now, I know that doing the following query works out like I want it to:
SELECT * FROM (
SELECT *
FROM charges
ORDER BY paid_on DESC
) as temp
GROUP BY id
My question: Is this the choice I have to take, or am I missing a key
thing so I only have to make one select statement? Unfortunately, this
Tuesday is Monday 2.0, so I'm not thinking as well as I should.
You can view the full mess here: http://pastebin.com/NfyaJ3ex
Also, if there's ways I can improve it, I'm open to suggestions.
--
-Jack Timmons
Blog: http://www.codeacula.com
Sandbox: http://www.trotlc.com
Twitter: @codeacula
Google Chat: Figure it out. C'mon, seriously.
More information about the thelist
mailing list