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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:57:09+00:00 2026-06-15T18:57:09+00:00

mysql> select * from orders; +———+————+————+————+———-+———-+ | orderid | customerid | orderdate | shipdate

  • 0
mysql> select * from orders;
+---------+------------+------------+------------+----------+----------+
| orderid | customerid | orderdate  | shipdate   | shipping | salestax |
+---------+------------+------------+------------+----------+----------+
|       1 |          1 | 2008-03-31 | 2008-03-31 |     4.99 |     5.00 | 
|       2 |          1 | 2008-04-01 | 2008-04-02 |     5.99 |     5.00 | 
|       3 |          2 | 2008-04-01 | 2008-04-02 |     3.99 |     6.00 | 
|       4 |          3 | 2008-04-02 | 2008-04-02 |     6.99 |     7.50 | 
+---------+------------+------------+------------+----------+----------+
4 rows in set (0.02 sec)
mysql> select * from orderinfo;
+---------+------------+-----+-------+----------+
| orderid | isbn       | qty | price | detailid |
+---------+------------+-----+-------+----------+
|       1 | 0929306279 |   1 | 29.95 |        1 | 
|       1 | 0929306260 |   1 | 49.95 |        2 | 
|       2 | 0439357624 |   3 | 16.95 |        3 | 
|       3 | 0670031844 |   1 | 34.95 |        4 | 
|       4 | 0929306279 |   1 | 29.95 |        5 | 
|       4 | 0929306260 |   1 | 49.95 |        6 | 
|       4 | 0439357624 |   1 | 16.95 |        7 | 
|       4 | 0670031844 |   1 | 34.95 |        8 | 
+---------+------------+-----+-------+----------+
8 rows in set (0.00 sec)

im trying to multiply the qty and price from the orderinfo table then add the shipping from the order table when i try to multiple them it works fine but when I add the shipping I get an incorrect value

this works

mysql> select orders.orderid,sum(orderinfo.qty*orderinfo.price) as order_total from orders
    -> inner join orderinfo
    -> on orders.orderid=orderinfo.orderid
    -> group by orderid;
+---------+-------------+
| orderid | order_total |
+---------+-------------+
|       1 |       79.90 | 
|       2 |       50.85 | 
|       3 |       34.95 | 
|       4 |      131.80 | 
+---------+-------------+
4 rows in set (0.00 sec)

this gives an incorrect value

mysql> select orders.orderid,sum(orderinfo.qty*orderinfo.price+orders.shipping) as order_total from orders
    -> inner join orderinfo
    -> on orders.orderid=orderinfo.orderid
    -> group by orderid;
+---------+-------------+
| orderid | order_total |
+---------+-------------+
|       1 |       89.88 | 
|       2 |       56.84 | 
|       3 |       38.94 | 
|       4 |      159.76 | 
+---------+-------------+
4 rows in set (0.00 sec)
  • 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-15T18:57:11+00:00Added an answer on June 15, 2026 at 6:57 pm

    You are applying the shipping rate inside the SUM() aggregate, when really you need to add it afterward, to the result of the SUM(). This has the effect of adding the shipping value for each row of the aggregated group, that is, double shipping for orderid = 1 and 4x shipping for orderid = 4.

    You can do this by adding shipping to the GROUP BY (actually MySQL doesn’t require this, but it is good practice).

    SELECT
      orders.orderid,
      /* Add `shipping` outside the SUM() aggregate */
      SUM(orderinfo.qty*orderinfo.price) + orders.shipping AS order_total 
    FROM orders
      INNER JOIN orderinfo ON orders.orderid = orderinfo.orderid
    GROUP BY
      orderid,
      shipping
    

    Here’s an example on SQLFiddle.com.

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

Sidebar

Related Questions

Possible Duplicate: MySQL ON vs USING? Query 1: SELECT * FROM users JOIN orders
I have this mysql query select id, FROM_UNIXTIME(`placedate`) as placedate_mysqldate from orders HAVING placedate_mysqldate
I have a mysql query Select * from tbl_schoolphotos where Filename like 'glasshouse_1_%' order
I have a MySQL query SELECT * FROM 'redirect' WHERE 'user_id'= \''.$_SESSION['user_id'].' \' ORDER
which is more efficient (when managing over 100K records): A. Mysql SELECT * FROM
In MySQL Select 1 from mytable and select null from mytable both return the
this is the mysql query SELECT * FROM users AS up JOIN users AS
I have the following MySQL. SELECT `outputtable`.`date`, count(*) as `count` FROM ( SELECT CONCAT(DATE(`mytable`.`starttime`),'
This mysql query doesn't seem to work for me: select * from myTable order
I am trying to insert rows into a MySQL database from an Access database

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.