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 8462063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:03:58+00:00 2026-06-10T14:03:58+00:00

Lets have a simple query: SELECT myfield FROM mytable WHERE criteria The above query

  • 0

Lets have a simple query:

SELECT myfield
FROM   mytable
WHERE  criteria

The above query gives us 10 rows of results for example. The field myField is a text field.

My question is this: is it possible to alter the above query and get the total word count of the myField field?

Edited: By total word count I mean the total word count of all selected fields in query, in all rows

Example

+------------------------------+-------+
| sentence                     | Words |
+------------------------------+-------+
| Hello World                  |     2 |
| Hello World                  |     2 |
| Mary had a little lamb       |     5 |
| Her fleece was white as snow |     6 |
| Everywhere that mary went    |     4 |
| Umm, sheep followed her      |     4 |
+------------------------------+-------+
  • 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-10T14:04:00+00:00Added an answer on June 10, 2026 at 2:04 pm

    Use the excellent function from this question by @otis in your query:

    mysql> select * from test;
    +----+------------------------------+
    | id | sentence                     |
    +----+------------------------------+
    |  0 | Hello World                  |
    |  1 | Hello World                  |
    |  2 | Mary had a little lamb       |
    |  3 | Her fleece was white as snow |
    |  4 | Everywhere that mary went    |
    |  5 | Umm, sheep followed her      |
    +----+------------------------------+
    6 rows in set (0.00 sec)
    
    mysql> SELECT sentence, wordcount(sentence) as "Words" from test;
    +------------------------------+-------+
    | sentence                     | Words |
    +------------------------------+-------+
    | Hello World                  |     2 |
    | Hello World                  |     2 |
    | Mary had a little lamb       |     5 |
    | Her fleece was white as snow |     6 |
    | Everywhere that mary went    |     4 |
    | Umm, sheep followed her      |     4 |
    +------------------------------+-------+
    6 rows in set (0.02 sec)
    

    To make the function work, you need to execute the declaration of the function in MySQL. It is just like executing any other query:

    mysql> DELIMITER $$
    mysql> CREATE FUNCTION wordcount(str TEXT)
                RETURNS INT
                DETERMINISTIC
                SQL SECURITY INVOKER
                NO SQL
           BEGIN
             DECLARE wordCnt, idx, maxIdx INT DEFAULT 0;
             DECLARE currChar, prevChar BOOL DEFAULT 0;
             SET maxIdx=char_length(str);
             WHILE idx < maxIdx DO
                 SET currChar=SUBSTRING(str, idx, 1) RLIKE '[[:alnum:]]';
                 IF NOT prevChar AND currChar THEN
                     SET wordCnt=wordCnt+1;
                 END IF;
                 SET prevChar=currChar;
                 SET idx=idx+1;
             END WHILE;
             RETURN wordCnt;
           END
         $$
    Query OK, 0 rows affected (0.10 sec)
    
    mysql> DELIMITER ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll use this simple query as an example: Select MyColumn From MyTable Where PrimaryKeyColumn
Lets suppose that I have the following simple query var q = from p
Let's say I have the following simple query SELECT TOP 1 name FROM months
I have a simple catalogue app that lets users download product data from an
I have a simple table (lets call it Table1) that has a NVARCHAR field
I have a basic MySQL query: $getFeed = SELECT posts.postID, posts.postTitle, ..... FROM posts
I have this query select rep, companyname,count(companyname) as [count], Commission from customers group by
I have a very large query that follows the format below: select ... from
Let's say I have a simple select query that returns the following: ID Name1
Lets say I have a simple ecommerce site that sells 100 different t-shirt designs.

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.