Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL MAX Command still returning more one result

I had this SQL Query working perfectly. I made a few small changes to the database, and reran it and it simply stopped work.

My goal here is to join master_followups with followups_fb_messages.message_text but only return a single row for each UID in master_followups with the most RECENT followups_fb_messages.message_text joined to it.

SELECT master_followups.*, 
  followups_fb_messages.message_text, 
  followups_fb_messages.message_time 
FROM
  master_followups 
  LEFT JOIN followups_fb_messages 
    ON master_followups.UID = followups_fb_messages.FID 
    AND followups_fb_messages.message_time = 
      (SELECT
        MAX(followups_fb_messages.message_time) 
        FROM followups_fb_messages 
        WHERE  followups_fb_messages.FID = master_followups.UID
      )

I've been trying to figure out what happened for the last hour.

Instead of returning 100 results as expected, this is returning 340. I checked the database and it shows 100 rows in master_followups and 340 rows in followups_fb_messages.

I had this exact query working fine about an hour ago, then it stopped. Maybe I changed something, but I can't see it.

like image 759
Ray Suelzer Avatar asked Feb 18 '26 21:02

Ray Suelzer


1 Answers

Try this one up.

SELECT  a.*, 
        b.*
FROM    master_followups a
        LEFT JOIN followups_fb_messages b
            ON a.UID = b.FID
        LEFT JOIN 
        (
            SELECT  FID, MAX(message_time) maxTime
            FROM    followups_fb_messages
            GROUP BY FID
        ) c ON b.FID = c.FID AND
                b.message_TIME = c.maxTime
like image 159
John Woo Avatar answered Feb 21 '26 09:02

John Woo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!