I have two tables :
- cart , cols are (painting_id, session_id, ip, user_agent)
- paintings , cols (painting_id, price)
Now I have to select the painting id from the table cart. I need to Join the two tables and get the sum of the price of all the paintings from table 'paintings'. Note the table cart doesnt have the price column , it has to be imported from the 'paintings' table. Only the sum of price of those paintings are shown which has been added into the cart table by a particular session id or email id.
Here is the query i have tried so far
SELECT p.SUM(price) FROM paintings
p JOIN cart c ON p.painting_id = c.painting_id
WHERE c.session_id = '$session'
It should be SUM(p.price) instead.
Try this query
SELECT cart.user_agent, sum(paintings.price)
from cart inner join
paintings on
cart.painting_id=paintings.painting_id
where session_id='$session'
if you want the total cost of all the users
SELECT cart.user_agent, sum(paintings.price)
from cart inner join
paintings on
cart.painting_id=paintings.painting_id
group by cart.session_id
Related
I have only those rows that have multiple rows in the same table. For example see below image.
In the above picture you can see 2 highlighted columns one is for useid(u_id) and second is for product id (product_id).
So you can see user id of 7 has multiple product like (78,40,44,45,53) and user id 9 has multiple products like (79,75,79) same like user id 40 has multiple products.
so want out put like if particular user have multiple products then it will display 'multiple product' in product name column instead of display all product name.
above picture display all product but i need to display 'multiple product' message instead of all products if particular user have multiple products
I have used following query but not getting result that i want .
SELECT *
FROM orderlist
WHERE product_id IN (
SELECT product_id
FROM orderlist
GROUP BY product_id
HAVING COUNT(*) > 1
)
You could do the JOINS after aggregation
SELECT *,
CASE WHEN c.Counts > 1 THEN 'Multi' ELSE o.ProductName END ProductName
FROM
(
SELECT product_id, COUNT(*) Counts
FROM orderlist
GROUP BY product_id
)c INNER JOIN orderlist o ON o.product_id = c.product_id
I have 2 tables for products what I need to do is, get product's name and description on basis of their attributes i.e. If table1's product's attribute1 is same as table2's product attribute1 then fetch product name and description of both the table. I'm litile confused with query, can somebody please help.
Here is my query:
select Product_Article_Number, Product_Description, Comments,
from P_Products as pp
WHERE EXISTS(select * from Competitor_Products as cc
WHERE(cc.CAttribute1 = pp.Attribute1
AND cc.CAttribute2 = pp.Attribute2
AND cc.CAttribute3 = pp.Attribute3
AND cc.CAttribute4 = pp.Attribute4))
result:
by this I'm getting details for table1 only, how to get other tables detail too.
I have the following DB structure
Table product (which holds my products information including the stock)
prod_id
prod_name
prod_desc
prod_price
stock
table store_orders_item (which is updated every time a customer buys from the my site)
id
order_id
sel_prod_id
sel_prod_price
sel_prod_qty
I need that every time I make a sale online and store_orders_items is updates, that reduce the stock field of the product sold on the table product
I think i need to use the code below, plus some sort of join or subquery to update my product table according to the prod_id, but i could not figure out how.
UPDATE product SET stock = stock - (SELECT SUM(sel_prod_qty) FROM store_orders_items);
I also tried:
UPDATE product
SET stock = stock - (SELECT s.sel_prod_qty, s.sel_prod_id, p.prod_id
FROM store_orders_items as s left join product as p ON
s.sel_prod_id=p.prod_id WHERE order_id=(SELECT MAX(order_id) from orders_number));
And got this error:
"You can't specify target table 'product' for update in FROM clause"
You must use join updates, for example in MySQL:
UPDATE
product p
INNER JOIN
store_orders_item s
ON
p.prod_id = s.sel_prod_id
SET
p.stock = p.stock - s.sel_prod_qty
WHERE
s.order_id = NNNNN
where NNNNN is the id of order just inserted.
I have two tables: tb_subjoe and tb_deliverydetails
I need to sum Price and Quantity column. at this my code mysql When I do
SELECT scode_id, scode_detail, deli_quote, SUM(deli_dequan) AS QUANTITY, SUM(deli_goodsprice) AS Good price, SUM(deli_wagesprice) AS Wage price
FROM tb_deliverydetails
LEFT JOIN tb_subjob
ON tb_subjob.scode_id = tb_deliverydetails.scode_id
GROUP BY `deli_quote`
it not answer for me
How do I write an sql query to display items from 2 tables based on a selection of a criteria which is from a different table.
Tables I have:
Customer table has columns CustomerID, Name, Address, Tel
CustomerOrder table has columns CustomerID, OrderID, Date, TotalAmount, Status
OrderItem table has columns OrderID, ProductCode, UnitPrice, Qty, TotalPrice
So when a CustomerID is selected, I want the orders to be displayed joining these 3 tables. so as below it should display all the orders that the customer has ever placed.
I tried using the query:
Select CustomerOrder.*, OrderItem.*
From CustomerOrder
INNER JOIN OrderItem Where Customer.CustomerID = $CustomerID
But it's not working. Need help in the query and also in displaying the data properly using php.
Can anyone help?
E.g.
CustomerID:__________
OrderID:__1____ Date:______ TotalAmount:______ Status:_____
ProductCode:__ UnitPrice:___ Qty:_____TotalPrice:__________
ProductCode:___ UnitPrice:______ Qty:_____ TotalPrice:_________
OrderID:___2___ Date:______ TotalAmount:______ Status:_____
ProductCode:__ UnitPrice:___ Qty:_____TotalPrice:__________
ProductCode:___ UnitPrice:______ Qty:_____ TotalPrice:_________
Since you don't need to display Customer information at this step, then you query can be :
SELECT co.OrderID
,co.Date
,co.TotalAmount
,co.Status
,ci.ProductCode
,ci.UnitPrice
,ci.Qty
,ci.TotalPrice
FROM CustomerOrder AS co
INNER JOIN OrderItem AS ci ON (ci.orderID = co.OrderID)
WHERE co.CustomerID = $customer
ORDER BY co.OrderID, ci.ProductCode
As you'd like to no repeat order information in your output, your PHP code should be soemthing like this:
$current_order_id = false;
foreach ($data as $row) {
if ($current_order_id!==$row['OrderID']) {
$current_order_id = $row['OrderID'];
echo "OrderID: ".$row['OrderID']." , Date: ".$row['Date']." , TotalAmount: ".$row['TotalAmount']." , Status: ".$row['Status']." <br>";
}
echo "ProductCode: ".$row['ProductCode']." , UnitPrice: ".$row['UnitPrice']." , Qty: ".$row['Qty']." , TotalPrice: ".$row['TotalPrice']." <br>";
}
Anothere way of doing the same is to first get all the Orders information from CustomerOrder only.
And then loop on the result and get items infotmation OrderItem for each order.
Try like this:
SELECT co.OrderID,date,TotalAmount,ProductCode, UnitPrice,Qty,TotalPrice,Status FROM CustomerOrder AS co
INNER JOIN OrderItem AS oi ON oi.orderID = co.OrderID
INNER JOIN Customer AS c ON c.CustomerID = co.CustomerID
WHERE c.CustomerID = $customer