This topic is locked

last record per group

11/24/2011 9:26:28 AM
PHPRunner General questions
B
bilboss author

how I can display the last record per group. I recommend using groupby?

example
id / group / news / date

1 / david / note1 / 11/10/2011

2 / felipe / Note2 / 11/11/2011

3 / david / nota8 / 11/15/2011

4 / andres / nota40 / 30/11/2011

5 / felipe / Note 80 / 12.01.2011
result

id / group / news / date

3 / david / nota8 / 11/15/2011

4 / andres / nota40 / 30/11/2011

5 / felipe / Note 80 / 12.01.2011
using groupby only shows me the first record in each group
thank you very much

C
cgphp 11/24/2011
SELECT *

FROM table_name

WHERE id IN (SELECT MAX(id) FROM table_name GROUP BY group)
B
bilboss author 11/24/2011

I returned the following message

"can not retrieve column information. Please modify sql query and try again.

http error message error "
My code is as follows
SELECT

Pro.group,

Pro.news,

Pro.date,

Pro.id

FROM Pro

WHERE Pro.id IN (SELECT MAX (Pro.id) FROM GROUP BY Pro.group Pro)

Sergey Kornilov admin 11/24/2011

What database do you use? Syntax may vary.

B
bilboss author 11/24/2011

mysql 5.1.59

B
bilboss author 11/24/2011

mysql 5.1.59

C
cgphp 11/24/2011
SELECT

Pro.group,

Pro.news,

Pro.date,

Pro.id

FROM Pro

WHERE Pro.id IN (SELECT MAX (Pro.id) FROM Pro GROUP BY Pro.group)
Sergey Kornilov admin 11/24/2011

Try to run this query in phpMyAdmin to see what error it returns and post it here.

B
bilboss author 11/24/2011

same mistake, that's the same code that I have, in the post above the digits wrong.
any idea what might happen?

C
cgphp 11/24/2011

Your query was wrong. Did you try the following version ?

SELECT

Pro.group,

Pro.news,

Pro.date,

Pro.id

FROM Pro

WHERE Pro.id IN (SELECT MAX (Pro.id) FROM Pro GROUP BY Pro.group)
B
bilboss author 11/24/2011

performs the query in phpmyadmin, but this is hung up, nose to estrare doing wrong, is there some other way to do it?

or have any other ideas?
thank you very much in advance

B
bilboss author 11/24/2011

Yes, I tried what you had this written

C
cgphp 11/24/2011

Could you post a screenshot of the "Tables" tab ?