Combine multiple 'orders' from mysql table and display total price? - php

I'm working on a panel to show all orders from our e-commerce site and the way I have the orders set up is to have a row for each order referring to the customer id.
I'm working on showing all open orders on our back-end however the rows are showing multiple rows for the same order if (so if the orderID is 18, and the order has 2 items ordered there a 2 rows all the with the orderID of 18).
I'll include some screenshots below so you have an idea of what's happening.
This is my sql statement to show all open orders:
function GetAllOpenOrders (){
global $conn;
$sql = "SELECT * FROM customer_orders LEFT JOIN ordered_products ON ordered_products.custOrderID=customer_orders.custOrderID WHERE customer_orders.orderOpen=1";
return $result = $conn->query($sql);
}
So again, I want to combine the orders with multiple products and display the total price in the open orders screen.

You need an aggregate query to display the total. Something like:
SELECT sum(prod.totalPrice) as TotalPrice
FROM customer_orders
LEFT JOIN ordered_products ON ordered_products.custOrderID=customer_orders.custOrderID
WHERE customer_orders.orderOpen=1
group by customer_orders.custOrderID
I don't have your database to test that query but that should point you in the right direction.

You need a sub-query where you SUM the order totals and then aggregate. You also can't add the "product ID" to your query otherwise it will by definition break it down into rows for each product ordered.
For example
select
customer_order_id,
sum(product_total_price) as order_total
from
(select
customer_order_id,
product_id,
product_total_price
from table
group by 1,2,3)
group by 1
If you're looking to show the names of the products ordered in your 1 row, I suggest using a CONCAT function for the product names (so it's all in one field) and then you'll have the total in 1 row.

Related

How can I order query by another table?

I have a long query, but I keep it simplified:
$query = $this->db->query(" SELECT id FROM oc_products ORDER BY ...? ");
Here is the problem. In this table I have all the products, but I have a second table, oc_seller_products, where I have same column ID, which match with oc_products.
I want to be ordered first id's which dont appear in oc_seller_products, and at last, appear id's which appear in oc_seller_products.
For example: in oc_products I have ID: 1,2,3,4,5
In oc_seller_products I have only ID: 4
I need to be ordered like this: 5,3,2,1 and the last: 4
I have a marketplace, so I want first my products to appear, on category page, then my sellers products.
I really have no idea how to do that.
select op.id
from oc_products op
left join oc_seller_products osp using (id)
order by osp.id is null desc, op.id desc
osp.id is null will be 1 when there is not an oc_seller_products record and 0 when there is, so sort by that, descending, first. And then after that, within those two categories, you seem to want descending id order.

retrieve top sellers from single table in mysql.?

I want to retrieve data from mysql of my client affiliated bestsellers for that i have table which store every order id and it's seller id also with amount of it,
table contains same name sellers so now how can i get select data in sequence of first show seller who num of rows are more and keep do it in desc order.
select DISTINCT MAX(reseller_id) FROM affilated_comission
here is mysql query i am using which is only showing one result i want it also shows who are maximum after top 1.
Something like this should do it.
SELECT
seller_id,
COUNT(volume) AS volume
FROM
table
GROUP BY
seller_id
HAVING
COUNT(volume) >= 10
ORDER BY
volume DESC

Sub query returns more than one row

I'm building a point of sale software in php
So I've saved the items in the cart in a temporary table called tempsales, then update all the quantity of items with the new quantities. Just one query, using the code below which gives me subquery, returns:
More than one row error
UPDATE items SET quantity = quantity -
(SELECT quantity FROM tempsales ORDER BY id ASC)
WHERE id IN
(SELECT id FROM ORDER BY I'd ASC)
It looks like you want to update a whole mess of rows in your items table based on the rows in your tempsales table. I think you want to match the id values. That requires using JOIN syntax in your UPDATE query, like this.
UPDATE items
JOIN tempsales ON items.id = tempsales.id
SET items.quantity = items.quantity - tempsales.quantity

MySQL sum up numbers in a relational database

I'm making a web application to make customers order items for anything. For that I've made a MySQL database which has the following tables:
customers
orders
order-items
products
In the customers table is all the information about the person such as:
The customer ID, for the primary key and auto increment (id)
The first name (first_name)
The last name (last_name)
The email address (email_adress)
Information about the customer (customer_info)
Example:
In the orders table is all the specific information about it such as:
The order ID, for the primary key and auto increment (id)
Which customer ordered it, linked with id field from the customers table (customer_id)
Order information (order_info)
The location where the order needs to go to (location)
When the order was created (created)
Example:
In the order-items table are all the items which every customer ordered, this is being linked by the order-id from the previous table.
The ID, for primary key and auto increment, not used for any relation (id)
The order ID, used for which product is for which order. This is linked with the id field from the orders table (order_id)
The product ID, this is used for what product they ordered, this is linked with the id field from the products table. (product_id)
The amount of this product they ordered (quantity)
Example:
In the products table is all the information about the products:
The ID, for primary key and auto incrementing, This is linked with the product_id field from the order_items table (id)
The name of the product (name)
The description of the product (description)
The price of the product (price)
Example:
The problem
Bob ordered product_id 2, times 3. Which is the sandwich with beef with the price of 2.50, which we have to multiply by 3 because it has been ordered 3 times. Which is 7.50
Bob also ordered product_id 3, times 5. Which is the sandwich with chicken with the price of 3.00, which we have to multiply by 5 because it has been ordered 5 times. Which comes out on 15.00
Now I need to sum these up. Which is 15.00 + 7.50 = 22.50
The question
How do I get the product_id linked with the actual price of the product_id? which I can then multiply by the quantity.
And then sum up all those values with the same order_id
For the first order we get product 2 (Quantity 3) and product 3 (Quantity 5), which should add 2.503 + 3.005 = 22.50
You asked, "
How do i get the product_id linked with the actual price of the product_id? Which i can then multiply by the quantity... And then sum up all those values with the same order_id."
Like this:
SELECT OI.Order_ID, Sum(OI.Quantity * P.Price) Total_Price
FROM `order-items` OI
INNER JOIN Products P
on OI.Products_Id = P.ID
GROUP BY OI.Order_ID
Expected output for sample data in question:
ORDER_ID Total_price
1 22.50
This is why I asked about sample Output. I'm not sure what columns you were trying to return so I returned just the sum total for each order.
Now what this says
Return a row for each order showing the order ID and the total_price which is the sum of (quantity ordered * price all lines for that order.
This is accomplished by looking at the order-items table (in back tic's because I'm not sure if mySQL like's dashes (-) in names.). Joining these tables based on the product_Id between the ordered items table and the price table. and then group the results by the ordered item allowing the sum to aggregate all the rows for an order times the price of the item on that line of the order together.
You can build an intermediate table that has the totals for each order, then JOIN that with the orders table to get the customerID of that order, which you join with customers to get the customer name
SELECT C.FirstName, C.LastName, Totals.Order_ID, Totals.NetCost
FROM (SELECT OI.order_id, SUM(OI.quantity * P.price) as NetCost
FROM orderitems as OI INNER JOIN products as P ON OI.products_id = P.id
GROUP BY OI.order_id
) as Totals
INNER JOIN orders as O on O.ID = Totals.order_id
INNER JOIN customers as C on C.ID = O.customer_id
And the problem #xQbert is talking about is that if your customer places an order on Monday, you raise your price on Tuesday, and run this report on Wed, the report will show a total different from what the customer saw when he approved the order. A better design would be to store orderprice in orderitem, or at least the ordertotal in orders so subsequent price changes don't affect historic orders. The former is better in case a customer wants a refund on one item.
EDIT: I mean the problem #xQbert mentions in his comment, his answer came in ahead of mine but I missed it on the refresh
EDIT 2: More explanation, as requested by #Bas
It's easiest to think of this query from the inside and work outward, so let me start with getting the prices of items.
I want to link an item in orderitem with it's price, so JOIN orderitem (I give it an alias of OI) to products (I give an alias of P) on the product ID. Product ID is called products_id in OrderItem, but just ID in products.
Once I have that, I can multiply the price from Products by the quantity in OrderItems to get a cost for just that item
Because I want the total of everything for each order, I use a GROUP BY clause on this sub query, with the thing I'm grouping by being order_id. With a GROUP BY, you usually take the field or fields you are grouping by, and some "aggregate" fields where you take the sum, or max, or average, or whatever of them, and it's the sum or whatever for all rows that are in the same group.
So at this point we have defined the query inside the parenthesis. I named that Totals, and it acts just like a real table, but it goes away when your query is over.
The next step is to combine this "totals" table with your orders table so we can look up the customer ID, and then combine it with Customers so we can look up the customer name. I used aliases for Customer and Orders (C and O respectively), but they are not necessary. In fact, this could be rewritten most if not all of the aliases. I think it makes it more readable, but if they confuse you, you could use the following instead, it's the same. I think Totals is necessary, but maybe not even that.
SELECT FirstName, LastName, Totals.Order_ID, Totals.NetCost
FROM (SELECT order_id, SUM(quantity * price) as NetCost
FROM orderitems INNER JOIN products ON products_id = products.id
GROUP BY order_id
) as Totals
INNER JOIN orders on orders.ID = Totals.order_id
INNER JOIN customers on customers.ID = orders.customer_id
Hopefully this is clear, but if not, leave more comments and I'll further explain tonight or tomorrow.

Query Between two date

SELECT product.pname,stock.pid,stock.qty,stock.rate
FROM product,stock
WHERE (date BETWEEN '2012-04-10' AND '2012-07-16') AND product.pid=stock.pid
This is my sql query but problem is when execute this query its show single result means my product table contains pid and pname and stock table contains pid,rate,qty and date.
I want to display record between two dates.
My query match with two records. But when i add "AND product.pid=stock.pid" its show only 1 record.
I want to display the product name from product table in the respect of pid of stock table.
Probably some product is not in the stock. Also I recommend you to do a JOIN like this:
SELECT product.pname,stock.pid,stock.qty,stock.rate
FROM product
LEFT JOIN
stock
ON product.pid=stock.pid
WHERE (date BETWEEN '2012-04-10' AND '2012-07-16')
I used LEFT JOIN that means to return products no matter if have or nor stock, I think that with this query you will get two Rows.
First try to get it working without the join:
SELECT stock.pid, stock.qty, stock.rate
FROM stock
WHERE stock.date BETWEEN '2012-04-10' AND '2012-07-16'
If that works, then use an outer to join to add information to each row without removing any rows.
SELECT product.pname, stock.pid, stock.qty, stock.rate
FROM stock
LEFT JOIN product ON product.pid=stock.pid
WHERE stock.date BETWEEN '2012-04-10' AND '2012-07-16'
The product name will be NULL if no product is found.
Try the below query.
SELECT product.pname,stock.pid,stock.qty,stock.rate
FROM product,stock
WHERE stock.date BETWEEN '2012-04-10' AND '2012-07-16' AND product.pid=stock.pid
Your selection criteria
WHERE ([stock.]date BETWEEN '2012-04-10' AND '2012-07-16')
makes it effectively RIGHT JOIN, only records that have/had stock within date range will be displayed. If you want to display ALL products irrespective of stock than you will need to add OR [stock.]date IS NULL to your selection criteria.

Categories