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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:01:10+00:00 2026-06-17T07:01:10+00:00

Look at this DB schema: The users can order products. For any order I

  • 0

Look at this DB schema:

DB Schema

The users can order products. For any order I add a new record in the orders table and a new record in orders-products N:M table for any single product ordered by the user (and fill pieces field). But, when a user creating an order, I need to show the list of all products and fill the pieces field with the quantity ordered for any product.

Usually, to do it, I use two queries.
The first query get all products in the products table:

SELECT * FROM products;

The second query gets the products ordered in the orders-products table filtering them by orders-idorder FK:

SELECT orders_idorder AS idorder, products_idproduct AS idproduct, pieces FROM orders_products WHERE orders_idorder=1;

Then I merge the results in a single array and use it to display the complete list of products with the pieces ordered for each one. The final result is something like this:

+---------+-----------+--------------+-------+--------+
| idorder | idproduct | description  | price | pieces |
+---------+-----------+--------------+-------+--------+
|       1 |         1 | Product 1    | 10.20 |      2 |
|       1 |         2 | Product 22   | 11.00 |   NULL |
|       1 |         3 | Product 333  | 19.22 |   NULL |
|       1 |         4 | Product 4444 |  9.20 |   NULL |
+---------+-----------+--------------+-------+--------+

Note: In the above example there are 4 records in the products table and just 1 record in the orders-products table (it has idorder=1, idproduct=1 and pieces=2).
-> Here you can find the SQL Dump to test the queries.

Merging arrays built from 2 queries is the best way?
I can do it with a single query?
What do you think about the performance?

  • 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-17T07:01:11+00:00Added an answer on June 17, 2026 at 7:01 am

    Generally letting the databse optimize the merge will be better than what you can do in code. The db is usually better at sorting too.

    How you structure the query depends on your desired result set. If you want all products regardless of whether they appear in the orders table then you’d use an OUTER JOIN otherwise an INNER JOIN would filter out products that have never been ordered.

    If you give us your desired results for some sample data we might be able to help you with the query, but give it a shot yourself first.

    set @searchOrderId = 1;
    select ifnull(op.orders_idorder, @searchOrderId) AS idOrder,
        p.idproduct,
        p.description,
        p.price,
        op.pieces
    from products p
    left join orders_products op on op.products_idproduct = p.idproduct
    where ifnull(op.orders_idorder, @searchOrderId) = @searchOrderId ;
    

    SQL Fiddle I was playing with to test it.

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

Sidebar

Related Questions

Ok so i have a pages table with all the static with this schema
I might be really off on this one but can anyone look over this
Background/context As schema.org is relatively new, perhaps this question will promote more discussion than
difficult to look this up if it has been asked previously since I don't
Not exactly sure how to look this up, but I'm not finding the solution
I have a problem with internet explorer 6 and 7, please look this example
look at this fiddle: http://jsfiddle.net/ugxNK/ I want that the first list element is in
Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!
Look ath this method of ThreadPoolExcecutor: public void execute(Runnable command) { ... if (runState
look at this code, <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <script> function change() {

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.