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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:11:44+00:00 2026-06-09T03:11:44+00:00

pardon my question title, I’m not sure what should I put it, I have

  • 0

pardon my question title, I’m not sure what should I put it, I have these two tables as below.

products                orders
+------+----------+     +--------+------+-------+
|  id  | name     |     |   id   |  qty |  pid  |
+------+----------+     +--------+------+-------+
|  1   | mouse    |     | 10001  |  20  |   1   |
|  2   | keyboard |     | 10002  |  15  |   3   |
|  3   | headset  |     | 10004  |  5   |   3   |
+------+----------+     | 10005  |  12  |   2   | 
                        | 10006  |  18  |   1   |
                        +--------+------+-------+

This is the LEFT JOIN query I am using and the output

SELECT p.id AS No, p.name AS ProductName, o.qty AS Quantity
FROM products AS p
LEFT JOIN orders AS o ON p.id = o.pid

+------+-------------+----------+
|  No  | ProductName | Quantity |
+------+-------------+----------+
|  1   | mouse       |   20     |
|  1   | mouse       |   18     |
|  2   | keyboard    |   12     | 
|  3   | headset     |   15     |
|  3   | headset     |    5     |
+------+-------------+----------+

What I am trying to achieve is an output as below:

+------+-------------+----------+
|  No  | ProductName | Quantity |
+------+-------------+----------+
|  1   | mouse       |   20     |
|      |             |   18     |
|  2   | keyboard    |   12     | 
|  3   | headset     |   15     |
|      |             |    5     |
+------+-------------+----------+

My question is it possible to do so? Any reply and suggestions is greatly appreciate. Thanks.

P/S: I also have tried using the GROUP_CONCAT(qty SEPARATOR “,”) but it returns the result in one row as I may have more additional column to add in the Orders table in the future and it will be difficult to read.

  • 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-09T03:11:47+00:00Added an answer on June 9, 2026 at 3:11 am

    In this case you can use MySQL variables. I store the previous product id in the variable @prev, and only if it changes we output the product name.

    http://www.sqlfiddle.com/#!2/d5fd6/9

    SET @prev := NULL;
    
    SELECT
      IF( @prev = p.id, NULL, p.id) AS No,
      IF( @prev = p.id, NULL, p.name) AS ProductName,
      o.qty AS Quantity
     ,@prev := p.id
    
    FROM products AS p
    
    LEFT JOIN orders AS o
           ON p.id = o.pid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pardon me for not finding a better title. Say I have two lists: list1
This may be very obvious question, pardon me if so. I have below code
Pardon the newbie question. My data model is really simple (two tables; 200 rows
Do pardon the simplicity of the question: mother Google seems not to the have
Pardon if this is a n00b question, I'm a n00b to grails... I have
Pardon the excessive amount of code, but I'm not sure if I can explain
I'm new to free-text search, so pardon the newbie question. Suppose I have the
Pardon me for the woolly question - I'm not really that familiar with OLAP
I am no expert in network security, so pardon if this question is not
Pardon me if this question sounds trivial, I don't have any experience working with

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.