|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Joining, grouping and ordering
HI guys and gals,
I really need some advice here - i have complete brain freeze over this problem. I have (among other things) two tables. One of these has customer details in it, the other is a log of all requests to the site. If a customer is logged in, then the log table records the customer id as well (or null otherwise). The log table was only ever designed to be used for generating basic stats reports but now my client wants to see when a customer last logged in. I have currently got this sql Code:
SELECT c.*, vh.time FROM customers c INNER JOIN visitor_history vh ON c.unid = vh.customer GROUP BY c.unid but when it groups it takes a very early 'time' (log on time) value. How can i get it to get the most recent? I think i may have to do some nested selects of use some other form of temporary table - but can't figure it out. Any thoughts? Many thanks in advance! |
|
#2
|
||||
|
||||
|
Think I've answered my own question
I think I've answered my own question
Code:
SELECT * FROM ( SELECT c.*, vh.time FROM customers c INNER JOIN visitor_history vh ON c.unid = vh.customer ORDER BY vh.time DESC, c.surname ) d GROUP BY d.unid done a quick test and it seems to work. If there are any quicker or more elegant methods, please do respond!!! |
|
#3
|
|||
|
|||
|
Luckily, this question is answered almost every single day. There's even a page in the manual about it. Just do a search for "groupwise max mysql".
|
|
#4
|
||||
|
||||
|
Quote:
GROUP BY together with the dreaded, evil "select star" is almost always a recipe for disaster without me going into details about why, just remember never to mix those two Quote:
Code:
SELECT MAX(vh.time) AS last_logged_in FROM visitor_history vh WHERE vh.customer = 937 |
|
#5
|
||||
|
||||
|
hey, I'm posting shorthand here and would rather not reveal full database schemas!!!!
"seems" yeah - not exactly what was asked for - the query is returning the last time each customer made a request while logged in - good enough, as a I can't be bothered to link onto the unique_visitors table to pull out session information And no, there is no requirement for a "where" statement as the log on date is to be displayed in the customer list, not the customer details page. There is a separate page containing all requests made by that customer with a different query which does not relate to this one!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Last edited by Northie : July 3rd, 2009 at 09:54 AM. |
|
#6
|
||||
|
||||
|
Quote:
oh, that's okay then, forget i said anything |
|
#7
|
||||
|
||||
|
Quote:
![]() |
|
#8
|
||||
|
||||
|
Quick update:
I had to change INNER JOIN to LEFT JOIN to get customers who had signed up but never logged in Just in case anyone is going to use the above SQL for their own needs |
|
#9
|
||||
|
||||
|
Ug, now they want to know if the customers have products ( which may or may not have been approved or not) and pagination!!!!
Code:
SELECT * FROM ( SELECT d.*, cp.has_products FROM ( SELECT c.*, vh.time FROM customers c LEFT JOIN visitor_history vh ON c.unid = vh.customer ORDER BY vh.time DESC, c.surname ) d LEFT JOIN ( SELECT customer, count(id) AS has_products FROM customer_products WHERE time_approved > 0 GROUP BY customer ) cp ON d.unid = cp.customer GROUP BY d.unid ) a ORDER BY surname ASC LIMIT 0, 50 |
|
#10
|
||||
|
||||
|
ORDER BY inside a subquery is useless
and the GROUP BY d.unid is going to arbitrarily pick an indeterminate vh.time value, if there is more than one, so it isn't necessarily going to be the last logged in time |
|
#11
|
|||
|
|||
|
Quote:
unless used in conjunction with an OFFSET and/or LIMIT clause |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Joining, grouping and ordering |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|