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

The Archive Base Latest Questions

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

I have a somewhat complicated question related to MySQL. This is the table I

  • 0

I have a somewhat complicated question related to MySQL. This is the table I have:

keyword   args     title    namespace_id      ratio
en        1        A        23                0.5
en        1        B        89                0.6
en        0        C        89                0.4
foo       1        Foo      23                0.7
bar       1        Bar      89                0.3

I want a list of all rows without duplicates of (keywords,args). If there are duplicates, the picked row should be selected by an order of namespace_ids I provide. The rows left should be ordered by ratio.

Example result with namespace_id order 23,89,x,y:

keyword   args     title    namespace_id      ratio
foo       1        Foo      23                0.7
en        1        A        23                0.5
en        0        C        89                0.4
bar       1        Bar      89                0.3

Example result with namespace order 89,23,x,y:

keyword   args     title    namespace_id      ratio
foo       1        Foo      23                0.7
en        1        B        89                0.6
en        0        C        89                0.4
bar       1        Bar      89                0.3

Is there any way I can to this directly in MySQL? I had a look at GROUP BY, ORDER BY and noticed even the GROUP_CONCAT() function but I didn’t manage to put it all together properly. The statement that does somehow I want is:

  SELECT keyword, args, title, namespace_id, ratio 
    FROM tbl 
GROUP BY keyword, args 
ORDER BY ratio DESC;

But now I don’t know how to bring in the namespace_id order.

I’ve found similar questions like this:
MySQL: "order by" inside of "group by"
The answer there close to what I want, however, my namespace_id order is varying and cannot be calculated by the MAX() function.

EDIT: The challenge here is to tell GROUP BY which row to pick. The normal ORDER BY apparently doesn’t, it only uses the output of GROUP BY.

  • 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-30T10:37:23+00:00Added an answer on May 30, 2026 at 10:37 am

    Try using FIELD() and a subquery:

    SELECT t1.keyword, t1.args, t1.title, t1.namespace_id, t1.ratio
    FROM tbl t1, (SELECT keyword, args, MIN(FIELD(namespace_id, 23, 89))
    minfield FROM tbl GROUP BY keyword, args) t2
    WHERE t1.keyword = t2.keyword AND t1.args = t2.args AND
    FIELD(t1.namespace_id, 23, 89) = t2.minfield
    ORDER BY ratio DESC;
    

    or a self-outer-join:

    SELECT t1.keyword, t1.args, t1.title, t1.namespace_id, t1.ratio
    FROM tbl t1
    LEFT OUTER JOIN tbl t2 ON
        t1.keyword = t2.keyword AND
        t1.args = t2.args AND
        FIELD(t1.namespace_id, 23, 89) < FIELD(t2.namespace_id, 23, 89)
    WHERE t2.keyword IS NULL AND t2.args IS NULL
    ORDER BY t1.ratio DESC;
    

    Edit: It might also be worth having a look at some (commercial) DBMS supporting OLAP operations (if you’re able to choose and if you are processing large sets of data). For Georg’s case I think the OVER and PARTITION BY keywords would have helped: http://publib.boulder.ibm.com/infocenter/rbhelp/v6r3/topic/com.ibm.redbrick.doc6.3/sqlrg/sqlrg36.htm#sii06377181

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

Sidebar

Related Questions

This is somewhat complicated to explain, so please bear with me. I have an
I have a method somewhat similar to the one written bellow(this is just a
[Note: This question had the original title " C (ish) style union in C#
Ok SQL and Oracle gurus I have a somewhat complicated query that I'm trying
This problem is somewhat complicated and I'm in a little over my head. I
I have a somewhat complicated assortment of tables for which I need to do
I'm somewhat new to NHibernate, so this question may be a no-brainer. Anyway, I'm
I'm not very good at using complicated MySQL, so I'm stuck with a somewhat
I have a somewhat complicated branching structure at work (at least for me). It
OK, I have a somewhat complicated system in C++. In a nutshell, I need

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.