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

  • Home
  • SEARCH
  • 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 7641439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:55:24+00:00 2026-05-31T08:55:24+00:00

This is my table definition: CREATE TABLE orders_total ( orders_total_id int unsigned NOT NULL

  • 0

This is my table definition:

CREATE TABLE orders_total (
  orders_total_id int unsigned NOT NULL auto_increment,
  orders_id int NOT NULL,
  title varchar(255) NOT NULL,
  text varchar(255) NOT NULL,
  value decimal(15,4) NOT NULL,
  class varchar(32) NOT NULL,
  sort_order int NOT NULL,
  PRIMARY KEY (orders_total_id),
  KEY idx_orders_total_orders_id (orders_id)
);

A populated table can look something like this (all made-up numbers from my memory):

1 - 13 - Shipping - $7.50   - 7.50   - ot_shipping - 2
2 - 13 - Tax      - $8.00   - 8.00   - ot_tax      - 4
3 - 13 - Total    - $56.67  - 56.67  - ot_total    - 3
4 - 14 - Shipping - $5.55   - 5.55   - ot_shipping - 2
5 - 14 - Discount - $6.40   - 6.40   - ot_discount - 1
6 - 14 - Coupon   - $10.00  - 10.00  - ot_coupon   - 5
7 - 14 - Total    - $150.25 - 150.25 - ot_total    - 3

Every order can have different classes, and not all orders have to have the exact number of classes. For example:

  1. Order 1 can have the following classes: ot_shipping, ot_tax, ot_total
  2. Order 2 can have the following classes: ot_shipping, ot_discount, ot_coupon, ot_total
  3. …

I’d like to write a query that queries my orders table (not displayed here), and joins the above table (orders_total), and grabs the “value” column only if the “class” is either “ot_total”, “ot_coupon”, or “ot_discount”.

Here is my attempt at creating such a query:

SELECT o.orders_id, o.customers_name, group_concat(ot.value) AS values
FROM orders o
LEFT JOIN orders_total ot
ON ot.orders_id = o.orders.id
GROUP BY o.orders_id

The above query would produce something like this:

First set of results:

orders_id      => 13
customers_name => John Doe
values         => 7.50,8.00,56.67

Second set of results:

orders_id      => 14
customers_name => Jane Roe
values         => 5.55,6.40,10.00,150.25

As you can see, the values gives me what I need, but I just don’t know what number belongs to which class. I’m only interested in ot_total, ot_discount, and ot_coupon.

Anybody can think of any solution?

Is it possible to somehow append the class name to the values as well? So I can have something like values => ot_shippimh7.50,ot_tax8.00,ot_total56.67

  • 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-05-31T08:55:25+00:00Added an answer on May 31, 2026 at 8:55 am

    You can do this:

    SELECT o.orders_id, o.customers_name,
           GROUP_CONCAT(CONCAT( ot.class, ot.value)) AS values
    FROM orders o
    LEFT JOIN orders_total ot
    ON ot.orders_id = o.orders.id
    WHERE ot.class in ("ot_total", "ot_coupon", "ot_discount")
    GROUP BY o.orders_id
    

    BTW: You can do better, by remove the class field from your orders_total, add a new table called Classes instead that contains a list of classes then add a foreign key reference to this table to your orders_total.

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

Sidebar

Related Questions

Table definition: CREATE TABLE IF NOT EXISTS `submenu_cat` ( `id` int(11) NOT NULL AUTO_INCREMENT,
In this MySQL table definition: CREATE TABLE groups ( ug_main_grp_id smallint NOT NULL default
The following table definition: CREATE TABLE Customers( id INT NOT NULL PRIMARY KEY, name
Table definition: CREATE TABLE [dbo].[AllErrors]( [ID] [int] IDENTITY(1,1) NOT NULL, [DomainLogin] [nvarchar](50) NULL, [ExceptionDate]
this table i want to create job_id dynamic Jobs Title text Job Description text
This table is used to store sessions (events): CREATE TABLE session ( id int(11)
Database is MySQL with MyISAM engine. Table definition: CREATE TABLE IF NOT EXISTS matches
I have a sqlite (v3) table with this column definition: timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
Got this: Table a ID RelatedBs 1 NULL 2 NULL Table b AID ID
I couldn't find anything about this in MySQL documentation. SELECT accesion_id, definition FROM accesion_table

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.