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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:49:51+00:00 2026-06-14T05:49:51+00:00

I have just finished this MySQL query and as I still consider myself a

  • 0

I have just finished this MySQL query and as I still consider myself a novice, was wondering if I have done this in the most efficient way?

I have 4 tables, the first 2 contain category data and category associations which need to be linked up to one another. The 3rd table contains product info and the 4th table contains prices; some products have more than one price and I only need the lowest price.

          **Category Data**
        cat_id        cat_name
    |:-----------|------------:|
    |  21        |  Cat One    |
    |  22        |  Cat Two    |


            **Category Associations**
         ca_id        cat_id        prod_id
    |:-----------|------------:|:------------:|
    |   1        |    21       |     83       |
    |   2        |    22       |     88       |


                **Product Info**
        prod_id      prod_name    brand_name
    |:-----------|------------:|:------------:|
    |   83       |  Prod One   |  Brand One   |
    |   88       |  Prod Two   |  Brand Two   |


             **Prices**
       prod_id      price_id    
    |:-----------|------------:|
    |   83       |  22.15      |
    |   83       |  25.66      |
    |   88       |  47.23      |
    |   88       |  12.22      |

and this is the data that I need as the output

    prod_id     prod_name     brand_name      cat_name     lowest_price
|:-----------|------------:|:------------:|:------------:|:------------:|
|   83       |  Prod One   |  Brand One   |  Brand Two   |  22.15       |
|   88       |  Prod Two   |  Brand Two   |  Brand Two   |  12.22       |

I have left out alot of the data and just given the basics for ease.

This query works; just wondering if it can be improved upon. The data will be stored in an array and will be sorted using javascript.

SELECT `".DB_PREFIX."products`.prod_id, `".DB_PREFIX."products`.prod_name, `" . DB_PREFIX . "products`.prod_brand, `" . DB_PREFIX . "categories`.cat_name, `" . DB_PREFIX . "products`.prod_image, `" . DB_PREFIX . "products`.prod_field_one, `" . DB_PREFIX . "products`.prod_field_two, `" . DB_PREFIX . "products`.prod_field_three, `" . DB_PREFIX . "products`.prod_field_four, `" . DB_PREFIX . "products`.prod_field_five, `" . DB_PREFIX . "products`.prod_field_six, MIN(`" . DB_PREFIX . "prices`.price) as LowestPrice
FROM (`".DB_PREFIX."products` 
INNER JOIN `".DB_PREFIX."category_associations` ON `".DB_PREFIX."products`.prod_id = `".DB_PREFIX."category_associations`.prod_id)
INNER JOIN `".DB_PREFIX."categories` ON `".DB_PREFIX."category_associations`.cat_id = `".DB_PREFIX."categories`.cat_id     
INNER JOIN `" . DB_PREFIX . "prices` 
ON `" . DB_PREFIX . "prices`.prod_id = `" . DB_PREFIX . "products`.prod_id 
GROUP BY `" . DB_PREFIX . "prices`.prod_id`

Thanks,
Simon

  • 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-14T05:49:52+00:00Added an answer on June 14, 2026 at 5:49 am

    Debug your query using EXPLAIN. Using EXPLAIN, you should be able to figure out if your query is utilizing the correct indexes or not.

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

Sidebar

Related Questions

I have just finished ridding my project of leaks, but there are still thousands
Well, I finally finished my site (Just learned PHP and MySQL) So I'm wondering
So I just finished learning JDBC this weekend and have successfully transferred my code
I have just finished creating an API where the requests from the API are
I have just finished creating an entire login and register systsem in PHP, but
All, I have a basic Windows 7 Phone application and I have just finished
I just finished reading the HTML5 Developer's Cookbook and have a question. I've read
I've just finished merging two branches that have been separated for a long time,
I have just started using android, and have about 5 layout files finished. However,
I've just finished my Android widget. Now I need to have different sizes of

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.