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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:10:48+00:00 2026-06-17T00:10:48+00:00

I am trying to get results from mysql by not letting a specific field

  • 0

I am trying to get results from mysql by not letting a specific field gets repeated more than 5 times.

For example, lets say that I have a table like this :

-------------
Name        City    
person1     Nashville
person2     Nashville
person3     Nashville
person4     Nashville
person5     Nashville
person6     Nashville
person7     New York
-------------

What I want it to return is this :

-------------
Name        City    
person1     Nashville
person2     Nashville
person3     Nashville
person4     Nashville
person5     Nashville
person7     New York
-------------

where the city field cannot be repeated more than five times! if it was repeated more than fix times, only return the first five results.

This question could be impossible (that is what I am guessing) but if there is a solution or anyway to work around what I am trying to achieve, please tell me! I can use PHP.

  • 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-17T00:10:49+00:00Added an answer on June 17, 2026 at 12:10 am

    How about something like this which uses variables to check the current row to the previous row, if they are the same then the row number will increase if they are not the row number will reset:

    select name, city
    from
    (
      select name, 
        city,
        @row:=(case when @prev=city then @row else 0 end) + 1 as rownum,
        @prev:=city pcity
      from yourtable
      order by city, name
    ) src
    where rownum <= 5
    

    See SQL Fiddle with Demo

    The result is:

    |    NAME |      CITY |
    -----------------------
    | person1 | Nashville |
    | person2 | Nashville |
    | person3 | Nashville |
    | person4 | Nashville |
    | person5 | Nashville |
    | person7 |  New York |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the correct formatting of results back from a Mysql
I am trying to get results from a MySQL database by searching multiple terms
I'm trying to create a json object from MySQL results, but not getting the
I'm trying to get results from a MySQL database (for invoices) that includes multiple
I am using Entity Framework. I am trying to get the results from linq
I am trying to get results from 3 tables, but its repeating the PART_ID
I am trying to use specific numeric results from a mysql_fetch_array within a while
Possible Duplicate: Mysql Offset Infinite rows I am trying to get all results for
Im trying to get data from a mysql database and display it in an
I am trying veru hard, to get results from a database to be displayed

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.