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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:53:37+00:00 2026-05-30T01:53:37+00:00

I have a mysql database, I want to select all values that are equal

  • 0

I have a mysql database, I want to select all values that are equal on “name” and “postcode”. And the query needs to select the most common data in the other fields.

If I have:

name postcode test  test2
a    a        1     2
a    a        1     2
a    a        2     1
a    a        1     1
a    a        1     1

Then this needs to return

a    a        1     1

Because (test)1 is 4 times in the table, and (test2)1 is there 3 times. So I need the most common data in the Column where the name and the postcode is the same.

  • 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-30T01:53:38+00:00Added an answer on May 30, 2026 at 1:53 am

    This is my first approach:

    select distinct
        name, 
        postcode, 
        (select 
           s.test
        from 
           your_table s
        where
           name s.name = m.name, s.postcode = m.postcode
        group by 
           s.name, s.postcode, s.test
        order by count(*) desc
        limit 1 ) as test,
        (select 
           s.test2
        from 
           your_table s
        where
           name s.name = m.name, s.postcode = m.postcode
        group by 
           s.name, s.postcode, s.test2
        order by  count(*) desc
        limit 1 ) as test2
    from your_table m
    

    If you don’t need high performance this is a solution. If this query is often performed then you should look for another approach.

    EDITED

    If you need more performance and also you need distinct rows, you can remove distinct and append group by name, postcode clause at the end of the query.
    Query looks like:

    select ... group by name, postcode
    

    This is not standard SQL but mysql allow this for better performance:

    Quoting MySQL doc:

    In standard SQL, a query that includes a GROUP BY clause cannot refer
    to nonaggregated columns in the select list that are not named in the
    GROUP BY clause. MySQL extends the use of GROUP BY so that the select
    list can refer to nonaggregated columns not named in the GROUP BY
    clause. This means that the preceding query is legal in MySQL. You can
    use this feature to get better performance by avoiding unnecessary
    column sorting and grouping.

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

Sidebar

Related Questions

I have a MySQL database that I want to archive . What is the
I have a MySQL database of keywords that are presently mixed-case. However, I want
I have mysql database and I want a software which can draw the database
I have a mysql database of entries with dates. So what I want is
I have trouble defining a trigger for a MySQL database. I want to change
I have stored various records in the MySql database orkut. Now I want to
I want to know how can i find all the values that are NULL
I'm working on a mysql query in a Drupal database that pulls together users
I have a fairly large mysql database and I would like to count all
if i have 4 variables and i want to select DISTINCT values form data

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.