Get the total price of an order
up vote
0
down vote
favorite
I need to calculate the total price of an order.
I am using this setup:
Orders
order_id
--------
1
2
3
Picklists
picklist_id order_id
----------- --------
1 1
2 1
3 2
4 3
Products
product_id picklist_id name price amount
---------- ----------- -------------- ----- ------
1 1 product A 100 1
2 1 product B 200 3
3 2 product C 300 2
4 3 product A 100 2
5 4 Product C 300 1
My question is, how do I calculate the total price for an order
So my outcome, ideally, would be something like this:
order_id total_price
-------- -----------
1 1200
2 200
3 300
EDIT:
Query so far:
SELECT
order_picklists.name,
FORMAT(SUM(price * amount), 2) AS total
FROM order_products
JOIN order_picklists ON order_picklists.id = order_products.picklist_id
GROUP BY order_picklists.name
This, however, only adds everything per picklist, but I need those combined for the order.
mysql
add a comment |
up vote
0
down vote
favorite
I need to calculate the total price of an order.
I am using this setup:
Orders
order_id
--------
1
2
3
Picklists
picklist_id order_id
----------- --------
1 1
2 1
3 2
4 3
Products
product_id picklist_id name price amount
---------- ----------- -------------- ----- ------
1 1 product A 100 1
2 1 product B 200 3
3 2 product C 300 2
4 3 product A 100 2
5 4 Product C 300 1
My question is, how do I calculate the total price for an order
So my outcome, ideally, would be something like this:
order_id total_price
-------- -----------
1 1200
2 200
3 300
EDIT:
Query so far:
SELECT
order_picklists.name,
FORMAT(SUM(price * amount), 2) AS total
FROM order_products
JOIN order_picklists ON order_picklists.id = order_products.picklist_id
GROUP BY order_picklists.name
This, however, only adds everything per picklist, but I need those combined for the order.
mysql
TryGROUP BY order_picklists.order_id
– Juan Carlos Oropeza
Nov 19 at 15:45
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to calculate the total price of an order.
I am using this setup:
Orders
order_id
--------
1
2
3
Picklists
picklist_id order_id
----------- --------
1 1
2 1
3 2
4 3
Products
product_id picklist_id name price amount
---------- ----------- -------------- ----- ------
1 1 product A 100 1
2 1 product B 200 3
3 2 product C 300 2
4 3 product A 100 2
5 4 Product C 300 1
My question is, how do I calculate the total price for an order
So my outcome, ideally, would be something like this:
order_id total_price
-------- -----------
1 1200
2 200
3 300
EDIT:
Query so far:
SELECT
order_picklists.name,
FORMAT(SUM(price * amount), 2) AS total
FROM order_products
JOIN order_picklists ON order_picklists.id = order_products.picklist_id
GROUP BY order_picklists.name
This, however, only adds everything per picklist, but I need those combined for the order.
mysql
I need to calculate the total price of an order.
I am using this setup:
Orders
order_id
--------
1
2
3
Picklists
picklist_id order_id
----------- --------
1 1
2 1
3 2
4 3
Products
product_id picklist_id name price amount
---------- ----------- -------------- ----- ------
1 1 product A 100 1
2 1 product B 200 3
3 2 product C 300 2
4 3 product A 100 2
5 4 Product C 300 1
My question is, how do I calculate the total price for an order
So my outcome, ideally, would be something like this:
order_id total_price
-------- -----------
1 1200
2 200
3 300
EDIT:
Query so far:
SELECT
order_picklists.name,
FORMAT(SUM(price * amount), 2) AS total
FROM order_products
JOIN order_picklists ON order_picklists.id = order_products.picklist_id
GROUP BY order_picklists.name
This, however, only adds everything per picklist, but I need those combined for the order.
mysql
mysql
edited Nov 19 at 15:39
asked Nov 19 at 15:30
Lanoye
32
32
TryGROUP BY order_picklists.order_id
– Juan Carlos Oropeza
Nov 19 at 15:45
add a comment |
TryGROUP BY order_picklists.order_id
– Juan Carlos Oropeza
Nov 19 at 15:45
Try
GROUP BY order_picklists.order_id
– Juan Carlos Oropeza
Nov 19 at 15:45
Try
GROUP BY order_picklists.order_id
– Juan Carlos Oropeza
Nov 19 at 15:45
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You should GROUP BY
the order_id
which will return only 1 row per order.
SELECT o.order_id, SUM(p.price * p.amount) AS total
FROM orders o
LEFT JOIN picklists pl ON pl.order_id = o.order_id
LEFT JOIN products p ON pl.picklist_id = p.picklist_id
GROUP BY o.order_id;
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You should GROUP BY
the order_id
which will return only 1 row per order.
SELECT o.order_id, SUM(p.price * p.amount) AS total
FROM orders o
LEFT JOIN picklists pl ON pl.order_id = o.order_id
LEFT JOIN products p ON pl.picklist_id = p.picklist_id
GROUP BY o.order_id;
add a comment |
up vote
1
down vote
accepted
You should GROUP BY
the order_id
which will return only 1 row per order.
SELECT o.order_id, SUM(p.price * p.amount) AS total
FROM orders o
LEFT JOIN picklists pl ON pl.order_id = o.order_id
LEFT JOIN products p ON pl.picklist_id = p.picklist_id
GROUP BY o.order_id;
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You should GROUP BY
the order_id
which will return only 1 row per order.
SELECT o.order_id, SUM(p.price * p.amount) AS total
FROM orders o
LEFT JOIN picklists pl ON pl.order_id = o.order_id
LEFT JOIN products p ON pl.picklist_id = p.picklist_id
GROUP BY o.order_id;
You should GROUP BY
the order_id
which will return only 1 row per order.
SELECT o.order_id, SUM(p.price * p.amount) AS total
FROM orders o
LEFT JOIN picklists pl ON pl.order_id = o.order_id
LEFT JOIN products p ON pl.picklist_id = p.picklist_id
GROUP BY o.order_id;
answered Nov 19 at 15:46
Jim Wright
3,9781524
3,9781524
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377869%2fget-the-total-price-of-an-order%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try
GROUP BY order_picklists.order_id
– Juan Carlos Oropeza
Nov 19 at 15:45