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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:40:53+00:00 2026-06-11T11:40:53+00:00

I spent several hours crafting an SQL query that executes a JOIN and sorts

  • 0

I spent several hours crafting an SQL query that executes a JOIN and sorts two columns together, in a way that I haven’t dealt with before. Here is the query:

SELECT `m`.`id`, `m`.`primary_category_id`, `m`.`primary_category_priority`, `m`.`description`
FROM (`merchant` AS m)
LEFT JOIN `merchant_category`
    ON `merchant_category`.`merchant_id` = `m`.`id`
WHERE
    `merchant_category`.`category_id` = '2'
    OR `m`.`primary_category_id` = '2'
GROUP BY `m`.`id`
ORDER BY
    LEAST(merchant_category.priority = 0, `primary_category_priority` = 0) ASC,
    LEAST(merchant_category.priority, `primary_category_priority` ) ASC
LIMIT 10

It has to sort two columns together, one from the merchant_category table, and one from the merchant table, so that they’re sorted together. Each row of the merchant has a a “primary” category, referred to directly in the table, and zero or more “secondary” categories, stored in the merchant_category table. Now it works fine, but it’s very slow: usually over a minute on my production database. I imagine the JOIN plus the complex sorting is causing the problem, but what can I do?

EDIT Here are the two table’s schemas:

CREATE TABLE IF NOT EXISTS `merchant` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) CHARACTER SET utf8 NOT NULL,
  `primary_category_id` int(11) NOT NULL,
  `primary_category_priority` int(10) unsigned NOT NULL DEFAULT '0',
  `description` mediumtext CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`id`)
)

CREATE TABLE IF NOT EXISTS `merchant_category` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `merchant_id` int(10) NOT NULL,
  `category_id` int(10) NOT NULL,
  `priority` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
)
  • 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-11T11:40:55+00:00Added an answer on June 11, 2026 at 11:40 am

    Try to add foreign key constraint on the second table,

    CREATE TABLE IF NOT EXISTS `merchant_category` (
      `id` int(10) NOT NULL AUTO_INCREMENT,
      `merchant_id` int(10) NOT NULL,
      `category_id` int(10) NOT NULL,
      `priority` int(10) unsigned NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`),
      CONSTRAINT mc_fk FOREIGN KEY (`merchant_id`) REFERENCES `merchant`(`id`)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent several hours with this SQL problem, which I thought would be
To begin, Ive spent several hours looking for an answer and have come CLOSE
Spent many hours and still can't figure that out. sampleData.json file contains the following
Just spent several hours writing up for a new site... looks great in my
I have spent several hours today reading up on doing Custom Routing in ASP.NET
I hope someone can help me as I've already spent several hours trying to
I spent several hours reading through docs and forums, trying to find a solution
I have now spent several hours trying to figure this out. I have this
I have spent the last several hours trying to get this to work and
I have spent many hours working on this program that is supposed to play

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.