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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.