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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:16:38+00:00 2026-06-10T13:16:38+00:00

I have a table containing data stored like: product_name | quantity | tert ASIZ

  • 0

I have a table containing data stored like:

product_name | quantity | tert
ASIZ A4 100 FOX | 20| BUYER1
CERVEZA A6 150 FOX | 15| BUYER1
ASIZ A4 150 FOX | 40| BUYER1   
CERVEZA A6 11 FOX | 15| BUYER1
QUINTA A6 150 FOX | 15| BUYER2
ASIZ A4 150 FOX | 33 | BUYER2
ASIZ A6 150 FOX | 15| BUYER2
CERVEZA A6 150 FOX | 15| BUYER2

I want to retrieve all data that matches first word (e.g. ASIZ) sum the quantities and display them like:

BUYER1
Asiz - 60 pcs.
Cerveza = 30 pcs.
etc.

BUYER2
Asiz = 48 pcs.
Cerveza = 15 pcs.

etc

at the moment I am using 2 while loops:

SELECT DISTINCT product_name 
FROM my_table 
WHERE buyer_code= '".$bcode."' 
GROUP BY product_name

— and I get the product names

then

SELECT DISTINCT product_name,tert, SUM(quantity) AS quantity 
FROM my_table 
WHERE buyer_code ='".$bcode."' AND 
      prouct_name LIKE '".$first_word_of_product_name."%' 
GROUP BY tert
ORDER BY data ASC

I get the sums right, but it lists them every time it finds similar named products (eg: BUYER1: ASIZ – 2 times etc.)

it shows:

Buyer1
ASIZ - 60
Cerveza - 30
ASIZ - 60
Cerveza - 30

I know it has something to do with the product_name, but how should I write the sql statement to work?
Thanks

  • 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-10T13:16:38+00:00Added an answer on June 10, 2026 at 1:16 pm

    Run

    SELECT tert, SUBSTRING_INDEX(product_name,' ',1) as product, sum(quantity) as qty
    FROM my_table 
    GROUP BY tert, product
    ORDER BY tert, product
    

    Your results are sorted by tert so ou can walk through one by one showing product and qty and displaying tert every time a tert changes.

    UPDATE:
    A code sample how to output the data (not tested):

    $tert = null;
    foreach($rows as $row){
        if($rows['tert'] != $tert){
            $tert = $rows['tert'];
            echo $tert;
        }
        echo $row['product'], "-", $row['qty'];
    }
    

    here $rows – all fetched rows.

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

Sidebar

Related Questions

I have a db table containing a column display_order . The data looks like
I have a table containing data about some users. Many of them use our
Let's say I have a table containing following data: | id | t0 |
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
Ok I have a data table containing duplicate Reciept numbers and a transaction value
I have a table containing hierarchical data. There are currently ~8 levels in this
I have a file where a data structure containing 6 columns is stored side
I have a data table containing multiple columns and one column that stores somewhat
I have a number of tables containing some basic (business related) mapping data. What's
I have a small database with tables containing a small amount of data which

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.