Jump to content

group by


jonniejoejonson

Recommended Posts

mId dSent origId 

 

1 jan14 4

2 jan4 4

3 jan21 7

4 jan11 7

5 jan8 7

 

I want the mId’s of the smallest dSent grouped by originalId

 

However the following query:

$query = "SELECT mId, origId, MIN(dSent) FROM products GROUP BY origId";

 

Should produce:

mId 2

mId 5

 

But is producing

mId 1

mId 3

 

It is simply selecting the mId of the first row of the grouped origId…

 

Any ideason how to get it to do what I want… thanks to any responders.

Link to comment
https://forums.phpfreaks.com/topic/117961-group-by/
Share on other sites

That is because you didn't add an ORDER BY

 

Also the dSENT isn't an integer  so how it treats it is as a string (http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_min)  see the link in there

 

SELECT mID as mID, origId as OrigID, MIN(dSent) as The_Min from `products` GROUP BY origID ORDER BY The_Min

 

Link to comment
https://forums.phpfreaks.com/topic/117961-group-by/#findComment-606802
Share on other sites

Thanks cooldude, but that is still selecting the wrong results, but ordering them differently...

 

In the example above dSent is a datetime stamp not a string... i just wrote as a string to make easier to show... but your right it is wrong.

However it is still selecting the wrong reslusts. your continued support is much appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/117961-group-by/#findComment-606814
Share on other sites

Table: messsages.

messageId

dateSent

originalMessageId

userId

1

jan14

4

3

 

2

jan4

4

3

 

3

jan21

7

3

 

4

jan11

7

3

 

5

jan8

7

3

 

 

(dateSent is a dateTimeStamp in mysql) just written it out to make it easier to see.

 

I want to select the messageId ’s of the most recent originalMessageId messages for userId’3’.

 

Therefore the table above should produce two messageId’s

3

1

 

Do you understand what I am trying to do? becuase it is quite hard to explain. thanks J

Link to comment
https://forums.phpfreaks.com/topic/117961-group-by/#findComment-606827
Share on other sites

thanks again cool dude, that works if the table stays like that, but doensn't do what i need...

this following sql statement seems to work:

 

$sql = "select messageId,originalMessageId,dateSent

FROM messages

WHERE userID='3' AND dateSent = (select max(dateSent) from messages as messages2 where messages2.originalMessageId = messages.originalMessageId)";

 

not sure how quick it is thugh... anyway if you hve a better idea let us know... thanks J.

 

 

Link to comment
https://forums.phpfreaks.com/topic/117961-group-by/#findComment-606874
Share on other sites

SELECT messageId from `messages` Where userId = '3' ORDER BY dateSent DESC LIMIT 0,2

 

Yes, that pulls the records 1 and 3 (although we really want 2 and 5), but wouldn't it be better to have a query that also worked for other sets of data, such as

[pre]

mId  dSent  origId

 

1    jan6      4

2    jan4      4

3    jan21    7

4    jan11    7

5    jan8      7[/pre]

Link to comment
https://forums.phpfreaks.com/topic/117961-group-by/#findComment-606882
Share on other sites

I hadn't seen your working one, the last one you posted before I posted mine was

 

$query = "SELECT mId, origId, MIN(dSent) FROM products GROUP BY origId";

 

Anyway, without actually timing them I wouldn't really know - and I dont have your database to do that.

 

Link to comment
https://forums.phpfreaks.com/topic/117961-group-by/#findComment-606993
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.