tags:

views:

86

answers:

4

In reference to my previous question: http://stackoverflow.com/questions/3082817/one-table-two-column-mysql-query-question

The command: "SELECT hostname, GROUP_CONCAT(DISTINCT name) as name FROM comments GROUP BY hostname"

returns the expected results:

192.168.0.1 user1,user2

192.168.3.5 user3

The same table "comments" also has a "timestamp" field. How do I include the latest (most current) timestamp for each user using that ip?

Example:

192.168.0.1 user1-1277146500,user2-1277250087

192.168.3.5 user3-1237150048

I tried a number of variations of the command, but could only get all the timestamps, not the most current one...

also.. if possible it would be nice to convert the epoch timestamp to something more friendly before displaying it.

A: 
MAX(`timestamp`)
zerkms
+4  A: 

Concat the varchar converted Max(timestamp) to the name and add timestamp in the Group by clause

SELECT hostname,
  GROUP_CONCAT(DISTINCT name, CAST(MAX('timestamp') AS VARCHAR) as name 
FROM comments GROUP BY hostname

Think it will work, though I have not tested this.

Chinjoo
like"GROUP_CONCAT(DISTINCT name,MAX(timestamp)) as name" ?I get a syntax error...
Justin Keogh
you need to cast the Max('timestamp') to the type of name, say if name is varchar then cast Max('timestamp') as varchar
Chinjoo
hm.. still trying.."SELECT hostname, GROUP_CONCAT(DISTINCT name,cast(MAX(timestamp) AS varchar)) as name FROM comments GROUP BY hostname"returns a syntax error...
Justin Keogh
I verified name is varchar
Justin Keogh
the current example in your comment: "SELECT hostname,GROUP_CONCAT(DISTINCT name, CAST(MAX('timestamp') AS VARCHAR) as name FROM comments GROUP BY hostname, timestamp" returns a syntax error "near 'VARCHAR) as name FROM comments GROUP BY hostname, timestamp'"
Justin Keogh
I tried it with MAX(timestamp) too
Justin Keogh
I added the missing ")".. still a syntax error......."GROUP_CONCAT(DISTINCT name, CAST(MAX('timestamp') AS VARCHAR))"
Justin Keogh
According to http://dev.mysql.com/doc/refman/5.1/de/cast-functions.html there is no `VARCHAR` for the cast. Try `CHAR` (or is char internally the same as varchar? Not sure about that)
DrColossos
"SELECT hostname,GROUP_CONCAT(DISTINCT name, CAST(MAX('timestamp') AS CHAR)) as name FROM comments GROUP BY hostname, timestamp" gived the error "ERROR 1111 (HY000) at line 1: Invalid use of group function"....
Justin Keogh
oops... if you group by both hostname and timestamp, it will be two different groups. You should only be doing group by hostname. The max timestamp will be taken from each group only. So just remove the timestamp from group by clause.
Chinjoo
A: 
 SELECT hostname, GROUP_CONCAT(DISTINCT name) as name, max(TIMESTAMP_FIELD) as timestamp_name FROM comments GROUP BY hostname
Vincent Ramdhanie
That's almost it, except it displays that last timestamp for that hostname, I want to display the last timestamp each username is listed with that hostname. This example only lists one timestamp like:192.168.0.1 user1,user2 1158169709
Justin Keogh
A: 
SELECT 
     hostname, 
     GROUP_CONCAT(CONCAT_WS("-", name, m_timestamp))
FROM
     (SELECT 
          hostname, 
          name,
          cast(max(timestamp) as char(24)) as m_timestamp 
     FROM 
          comments 
     GROUP BY 
          hostname,
          name) as A
GROUP BY 
     hostname

Edit

Updated column name to timestamp (was c_timestamp) per OP's comments

potatopeelings
I changed c_timestamp to timestamp after getting "ERROR 1054 (42S22) at line 1: Unknown column 'c_timestamp' in 'field list'" and it worked perfectly after that. Thank you!
Justin Keogh
Made the same change in the original query too. Cheers!
potatopeelings