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

  • SEARCH
  • Home
  • 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 6587417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:54:51+00:00 2026-05-25T16:54:51+00:00

I have the following mysql code in a view SELECT threadid, SUM(LENGTH(pagetext)-LENGTH(REPLACE(pagetext, ‘ ‘,

  • 0

I have the following mysql code in a view

SELECT 
threadid,
SUM(LENGTH(pagetext)-LENGTH(REPLACE(pagetext, ' ', ''))+1)

FROM post
GROUP by threadid

This count the number of words in each column with the same “threadid”, and return the results grouped by the threadid. It works fine, but I want to limit the words counted only for the first 5 rows in each column. How can this be achieved?

Table Structure

+-----------------+-------------+
| threadID        | pagetext    |
+-----------------+-------------+
| 3               | some text   |
| 3               | other text  |
| 5               | text        |
+-----------------+-------------+

The view returns

+-----------------+-------------+
| threadID        | count       |
+-----------------+-------------+
| 3               | 4           |
| 5               | 1           |
+-----------------+-------------+

I’m looking to limit the count number to only n rows for each threadID. For example, if I limit the count to only 1 row, the threadID “3” will have a “count” of 2.

  • 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-05-25T16:54:52+00:00Added an answer on May 25, 2026 at 4:54 pm
    SELECT
        threadid,
        SUM(LENGTH(pagetext)-LENGTH(REPLACE(pagetext, ' ', ''))+1) AS len
    FROM (
        SELECT
            p1.threadid,
            p2.pagetext,
            COUNT(*) AS num
        FROM post AS p1
        JOIN post AS p2 ON p1.threadid=p2.threadid AND p1.postid <= p2.postid
        GROUP BY p1.threadid, p2.pagetext
        HAVING num<=5
    ) a
    GROUP BY threadid
    

    EDIT: Best way could be, that there is an auto-incremented column (I’ll name it postid), which helps us to get the first five posts for each thread.

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

Sidebar

Related Questions

I have the following little bit of MySQL code CREATE VIEW UserResults AS SELECT
I have the following MySQL code: SELECT COUNT(e.eid) AS cnt, e.c_id, c.cdesc FROM e
I have the following MySQL query that I execute from a .php page SELECT
I have the following code to insert items into a MySQL database from an
I have the following sample query (MySQL): SELECT * FROM `action` WHERE `customer_id` IN
I have the following code: Imports MySql.Data.MySqlClient Imports MySql.Data.Types Public Class FormMain Private Sub
I have the following table structure (in MySQL): DocID, Code, IsDup, DopOf , where
I have code with the following form: <?php function doSomething{ //Do stuff with MySQL
Any idea why the following code is not working mysql credentials are correct, have
I currently have the following MySQL statement to replace the HTML entity for a

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.