Jump to content

Recommended Posts

Hi,

 

MySQL Version - 5.1.46

 

Ive been tearing my hair out over what I consider a relatively easy MySQL query for 3 days now to no avail. 

 

I want the last Record of each Group By. 

 

I am hoping somebody here can help, belowis the query in its current form. Below that Ive attached images of what the returned record set looks like in its current form, and a picture of what I want it to look like.

 

Please ignore the duplicate columns,  I included them when I was trying 'debug' my SQL.

 

SELECT
c.CatagoryID                        AS 'Catagory ID',
c.CatagoryTitle                     AS 'Catagory Title',
c.CatagorySubject                   AS 'Catagory Subject',
COUNT(DISTINCT mCount.MessageID)    AS 'Total Messages',
COUNT(DISTINCT pCount.PostID)       AS 'Total Posts',
Hope.*

FROM 

onecall.sv2_department_members dm, 
onecall.sv2_department_rules dr, 
onecall.sv2_message_catagory c, 
onecall.sv2_message_message mCount, 
onecall.sv2_message_post pCount

JOIN 
(
    SELECT 
    p.PostID, 
    p.MessageID AS 'Post MsgID',
    m.MessageID, 
    m.CatagoryID, 
    p.PostTime, 
    p.Post 
    FROM onecall.sv2_message_post p, onecall.sv2_message_message m 
    WHERE m.MessageID = p.MessageID
    GROUP BY p.PostID
) AS Hope

WHERE 

dm.UserID = '168' AND 
dr.DepartmentID = dm.DepartmentID AND 
c.CatagoryID = dr.CatagoryID AND 
mCount.CatagoryID = c.CatagoryID AND 
pCount.MessageID = mCount.MessageID AND
Hope.CatagoryID = c.CatagoryID
    
GROUP BY Hope.CatagoryID, Hope.PostID
ORDER BY Hope.CatagoryID, Hope.PostID ASC

 

I wish it to return the following -

help.jpg

 

Also the EXPLAIN statement -

help2.jpg

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/215951-last-id-record-of-each-group-by/
Share on other sites

Yeah, I only used the Hope.* to save me a bit of typing to otuput all fields from that table. But even when I specifically selecr the columns I need from table Hope it still gives the same result set, so thats not the problem.

 

I really need to move on with this so for now Ive opted to use -

 

SELECT
c.CatagoryID                        AS 'Catagory ID',
c.CatagoryTitle                     AS 'Catagory Title',
c.CatagorySubject                   AS 'Catagory Subject',
COUNT(DISTINCT mCount.MessageID)    AS 'Total Messages',
COUNT(DISTINCT pCount.PostID)       AS 'Total Posts',
Hope.PostID                         AS 'Hope.Post ID',
Hope.MessageID                      AS 'Hope.Message ID',
p.PostID                            AS 'p.Post ID',
p.MessageID                         AS 'p.Message ID',
Hope.PostTime,
Hope.Post

FROM 

onecall.sv2_department_members dm, 
onecall.sv2_department_rules dr, 
onecall.sv2_message_catagory c, 
onecall.sv2_message_message mCount, 
onecall.sv2_message_post pCount

JOIN 
(
    SELECT 
    p.PostID, 
    m.MessageID,
    m.CatagoryID, 
    p.PostTime, 
    p.Post 
    FROM onecall.sv2_message_post p, onecall.sv2_message_message m 
    WHERE m.MessageID = p.MessageID
    
) AS Hope 

LEFT OUTER JOIN
onecall.sv2_message_post p ON (p.PostID > Hope.PostID)

WHERE 

dm.UserID =         '168' AND 
dr.DepartmentID =   dm.DepartmentID AND 
c.CatagoryID =      dr.CatagoryID AND 
mCount.CatagoryID = c.CatagoryID AND 
pCount.MessageID =  mCount.MessageID AND
Hope.CatagoryID =   c.CatagoryID 

    
GROUP BY Hope.CatagoryID, Hope.PostID
ORDER BY Hope.CatagoryID, Hope.PostID ASC

 

From the resultset generated I can use PHP to do the rest of the filtering.

 

Feels very hacky considering all I wanted is the last row of each Hope.CatagoryID, will come back to it at a later date when I have more time to spend on it. From what I have read online, all other major databases have a very 'easy' function to do what I require, only MySQL seems to be the outsider on this one.

 

Thanks for your reply anyway.

Further to this Ive just got my desired resultset by adding a 'CatagoryID' field to my post Table. I didn't want the extra field there but it seems to be the only way to resolve my problem in one query. Never know, might aid me in the long run.

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.