Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8474689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:41:39+00:00 2026-06-10T17:41:39+00:00

In MySql you can use the MAX() function to get the highest value when

  • 0

In MySql you can use the MAX() function to get the highest value when using GROUP BY, how can I do the same thing to get the longest string of text?

Sample table:

id_|_post_id|_title__________|_body_____________________________________________
1  | ZXBF1J | Favorite Color | My favorite color is blue.
2  | ZXBF1J | Favorite Color | My favorite color is blue, no wait...
3  | ZXBF1J | Favorite Color | My favorite color is blue, no wait, yelloooow!
4  | AR3D47 | Quest          | To seek..
5  | AR3D47 | Quest          | To seek the Holy
6  | AR3D47 | Quest          | To seek the Holy Grail.

The tricky part is that I want to ORDER BY id ASC to see the oldest entries on the top, and I want to group by the post_id which is not something that I can use to ORDER, and get the longest body.

Sample query:

SELECT post_id, title, MAX(body) // obviously MAX() doesn't work here
FROM posts
GROUP BY post_id
ORDER BY id ASC

Desired output:

post_id|_title__________|_body_____________________________________________
ZXBF1J | Favorite Color | My favorite color is blue, no wait, yelloooow!
AR3D47 | Quest          | To seek the Holy Grail.

Again the key is to select the longest body while maintaining the order based on the id.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:41:40+00:00Added an answer on June 10, 2026 at 5:41 pm

    You need use CHAR_LENGTH instead of LENGTH

    SELECT a.id, a.post_id, a.body
    FROM posts a INNER JOIN
    (
        SELECT post_ID, title, MAX(CHAR_LENGTH(body)) totalLength
        FROM posts
        GROUP BY post_ID, title
    ) b ON a.post_id = b.post_ID AND
            a.title = b.title AND
            CHAR_LENGTH(a.body) = b.totalLength
    

    You might want to see the difference: CHAR_LENGTH( ) vs LENGTH( )

    SQLFiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get mysql gem installed for use on rails, using jruby, can't
I have a problem regarding the datediff MYSQL function, I can use it and
In MySQL you can use the syntax DELETE t1,t2 FROM table1 AS t1 INNER
How can one use Triggers for Logging History of database changes in MySQL? If
How can we re-use the deleted id from any MySQL-DB table? If I want
Can I set rails to use mysql with MEMORY as the DB engine? I
Why can't the data in mysql not updated when I use checkbox, I already
I use a Mahout (version 0.8) and MySQL. I can not figure out how
I'd like to use MySQL from SilkJS under OSX Lion, but can't find the
I'm trying to use ON CASCADE DELETE in mysql db but I can't make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.