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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:01:12+00:00 2026-05-15T14:01:12+00:00

I have several tables that get JOINed together to form a table with columns

  • 0

I have several tables that get JOINed together to form a table with columns

designID
garmentID
colorID
sizeID
imageID

I have a function that looks like this [variables in square brackets are optional]:

getProductImages($designID, [$garmentID], [$colorID], [$sizeID]);

I want it to return all imageIDs that match $designID in the following order:

  • Rows that match $garmentID, $colorID, and $sizeID first
  • Rows that match $garmentID and $colorID next
  • Rows that match just $garmentID next
  • Rows that match none (just $designID) last

I could do this pretty easily by just loading all the rows that match $designID and then sorting them in PHP, but my understanding is that it’s generally faster to do sorting in MySQL when possible. There will be about 20 rows matching a given $designID.

So my question is twofold: Is it worth doing the sorting in a SQL statement? If I do, what is the best approach to take?

I would also be very interested to know if there is a name for this kind of sorting.

  • 1 1 Answer
  • 1 View
  • 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-15T14:01:13+00:00Added an answer on May 15, 2026 at 2:01 pm

    If I understood correctly, it looks like you can use expressions in your ORDER BY, in a way similar to the accepted answer given to the following Stack Overflow post:

    • Using MySql, can I sort a column but have 0 come last?

    Therefore, your query might look like this:

    SELECT    imageID
    FROM      ...
    JOIN      ...
    WHERE     designID = 100          
    ORDER BY  garmentID = 1 DESC,
              colorID = 5 DESC,
              sizeID = 10 DESC;
    

    Note that garmentID, colorID, and sizeID are not used as filters in the WHERE clause. The values are only used in the ORDER BY expressions.

    Test case:

    CREATE TABLE designs (designID int, garmentID int, colorID int, sizeID int);
    
    INSERT INTO designs VALUES (100, 1, 1, 1);
    INSERT INTO designs VALUES (100, 1, 2, 2);
    INSERT INTO designs VALUES (100, 1, 5, 3);
    INSERT INTO designs VALUES (100, 1, 5, 10);
    INSERT INTO designs VALUES (100, 1, 5, 15);
    INSERT INTO designs VALUES (100, 1, 8, 20);
    INSERT INTO designs VALUES (100, 2, 5, 10);
    INSERT INTO designs VALUES (100, 2, 6, 15);
    INSERT INTO designs VALUES (101, 1, 1, 1);
    INSERT INTO designs VALUES (101, 2, 1, 1);
    

    Result:

    SELECT    * 
    FROM      designs 
    WHERE     designID = 100 
    ORDER BY  garmentID = 1 DESC, 
              colorID = 5 DESC, 
              sizeID = 10 DESC;
    
    +----------+-----------+---------+--------+
    | designID | garmentID | colorID | sizeID |
    +----------+-----------+---------+--------+
    |      100 |         1 |       5 |     10 |
    |      100 |         1 |       5 |      3 |
    |      100 |         1 |       5 |     15 |
    |      100 |         1 |       1 |      1 |
    |      100 |         1 |       2 |      2 |
    |      100 |         1 |       8 |     20 |
    |      100 |         2 |       5 |     10 |
    |      100 |         2 |       6 |     15 |
    +----------+-----------+---------+--------+
    8 rows in set (0.02 sec)
    

    Note how the row that matches the specified garmentID, colorID and sizeID is first. Failing that, the rows that match garmentID and colorID are next. Then the rows that only match garmentID follow. Then the rest, which only match the designID filter of the WHERE clause.

    I believe it is worth doing this in SQL. As @Toby noted in the other answer, in general you don’t need to worry about performance when sorting such a small number of rows, assuming you will always be filtering by designID… As for your other question, I don’t know if there is a name for such a query – I tend to call it “ordering by an expression”.

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

Sidebar

Related Questions

I have joined together several tables to get data i want but since I
I'm using Castle ActiveRecord in a warehouse project. I have several tables that get
I have several tables in my database that have read-only fields that get set
I have several tables that I am trying to get some data out of,
I have several tables that I am selecting data from. I am trying to
I have several tables that I want to put side by side, however when
I have several database tables that just contain a single column and very few
I have a function that updates several tables. As an example, let's say it
I have a data-upload function that load some data into several tables and processes
I have an Excel Workbook that has several worksheets in it. The tables in

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.