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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:28:51+00:00 2026-05-25T09:28:51+00:00

I like to select a row by priority on MySQL. I don’t know if

  • 0

I like to select a row by priority on MySQL. I don’t know if it is possible, but see the example below:

id name
1  John
2  Doe
3  Mary
4  Jhonny

Supposing that I don’t know the ID, but by a specific reason, I need select by priority: 3, 2, 1… On this case, the MARY will be selected (#3). But if I will need select by order 5, 2, 1, the DOE will be selected (#2).

Ok, I can do it by using IN(5, 2, 1). The problem is that if I don’t have any results (like IN(5, 6, 7)), I need at least one row (don’t matter what).

For instance: select 5, 6, 7… nothing found… then, select the first found (like JOHN).

It’s possible?

Bye!

Edit: I just thought of this solution, but I do not know how fast it should be, but it works well. Accept a response that has a better benchmark for nobody lose.

ORDER BY FIND_IN_SET(`id`, '5,6,7') DESC
  • 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-25T09:28:52+00:00Added an answer on May 25, 2026 at 9:28 am
    SELECT *
    FROM your_table
    ORDER BY 
    (CASE id 
    WHEN 5 THEN 1
    WHEN 6 THEN 2
    WHEN 7 THEN 3
    ELSE id+10
    END)
    LIMIT $some_limit;
    

    The trick is make use on order by

    So, the matching id of 5 will give the priority 1,
    matching id of 6 will give the priority 2,
    matching id of 7 will give the priority 3,
    otherwise, least id will return

    assuming you are using unsigned for id

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

Sidebar

Related Questions

In MySQL with REPEATABLE_READ isolation mod, my transaction is like this: select a row
I'd like to select the maximum row of a group, but I'd like the
I would like to select a row from 2 different tables that are relational.
the row source for a listbox looks like this: SELECT users.id, users.first, users.last, chavrusas.luser_type
I have a simple table of data, and I'd like to select the row
I'd like to select the first row of each set of rows grouped with
I'd like to display a Details view from a select row event on a
I have a SQL query like this : SELECT myXmlField.value('(./row/@myAttribute)[1]', 'nVarchar(max)') as myAttribute FROM
I have a selection that will select just 1 row of category. but I
I'm trying to do something like 'select groupwise maximum', but I'm looking for groupwise

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.