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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:54:13+00:00 2026-05-20T21:54:13+00:00

I have a problem in mySQL that goes as follows: Count the instances of

  • 0

I have a problem in mySQL that goes as follows:

  • Count the instances of a substring in a string field in a mySQL database
  • Order the results by the number of occurrences of that substring (DESC)

I have never done anything other than rudimentary queries.. I can’t find a solution elsewhere.

  • 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-20T21:54:13+00:00Added an answer on May 20, 2026 at 9:54 pm
    SELECT (CHAR_LENGTH(str) - CHAR_LENGTH(REPLACE(str, substr, ''))) / CHAR_LENGTH(substr) AS cnt
    ...
    ORDER BY cnt DESC
    

    Yep, looks bloated but afaik there is no any other possible solution.

    mysql> select (CHAR_LENGTH('asd') - CHAR_LENGTH(REPLACE('asd', 's', ''))) / CHAR_LENGTH('s');
    +-----------------------------------------------------------------+
    | (CHAR_LENGTH('asd') - CHAR_LENGTH(REPLACE('asd', 's', ''))) / CHAR_LENGTH('s') |
    +-----------------------------------------------------------------+
    |                                                          1.0000 |
    +-----------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    
    
    mysql> select host, (CHAR_LENGTH(host) - CHAR_LENGTH(REPLACE(host, 'l', ''))) / CHAR_LENGTH('l') AS cnt from user;
    +-----------+--------+
    | host      | cnt    |
    +-----------+--------+
    | 127.0.0.1 | 0.0000 |
    | honeypot  | 0.0000 |
    | honeypot  | 0.0000 |
    | localhost | 2.0000 |
    | localhost | 2.0000 |
    +-----------+--------+
    5 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problem with MySQL Workbench in that I sometimes can't set foreign
I have a mysql problem, my query looks as follows but not complete SELECT
I have a bit of a strange problem with MySQL that I've never seen
I have one field that is indexed as string in Solr's schema.xml, which is
I have a dataset obtained from MySQL that goes like this: Array ( [0]
We have a product that uses a MySQL database as the data-store. The data-store
I have run in to a slight problem. The story goes as follows: I
I have an ajax call to a php script that goes into a database
I have the problem, that MSSQL Server 2000 should select some distinct values from
The problem I have is that it is giving me a warning in mysqli

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.