Get Min Price From Product Variation Table in MySQL - mysql

Product Table
Variation Table
How do I get a result from Product with every product's min price from Variation?

I join by description, which should work if no different products have the same description. It would be better to join by something more specific like product_id, but product_table does not have that as a column.
SELECT
p.*,
v.min(price) as min_price,
FROM
variation_table v
JOIN
product_table p ON v.description = p.description;
You could probably add ORDER BY product_id to do in order of the product ids or whatever other field you'd prefer.

Related

Join Two Tables based on a third table's data

I have 3 tables, for the sake of this exercise we'll call them: Products, Price, and Discount. I'm trying to join Products and Price tables, only if the ProductID is found in Discount.ProductID (ProductID column within the Discount table).
Products:
ProductID
Size
Color
Ref#
A1234
Small
Blu
0C94
B5678
Med
Red
1D96
Price:
Ref#
Base
Tax
0C94
3.48
0.96
Discount:
ProductID
List
Site
A1234
Two
Three
I'm familiar with joins, so my code starts off as:
SELECT * FROM Product as a
left join Price as b
on a.Ref# = b.Ref#
but I've never nested a constraints within a where clause (if that's even the correct approach) based on a third table. Any advice would be greatly appreciated. The end result would be a new products table that only shows the one product, because ProductID B5678 is not in the Discount table.
Just do a 3-table join.
SELECT DISTINCT a.*, b.*
FROM Product AS a
JOIN Price AS b ON a.`Ref#` = b.`Ref#`
JOIN Discount AS c ON a.ProductID = c.ProductID
If you don't need any of the contents of the Discount table, use the exists() funtion to execute a sub query in the where clause. This will give you the fastest results.
SELECT *
FROM Product as a
left join Price as b on a.Ref# = b.Ref#
WHERE EXISTS (
SELECT *
FROM Discount as c
WHERE c.ProductID = a.ProductID
)
If however you do need one or more of the columns of Discount, do an inner join between Product and Discount, joining them on the ProductID. This will result in only the products that have discount, and then do another left join to Price to get the columns from Price into the resultset too. Do be aware though that in case multiple rows exist in Discount for the one Product row, this will result in the same product shown on multiple rows.
SELECT *
FROM Product as a
inner join Discount as c on c.ProductID = a.ProductID
left join Price as b on a.Ref# = b.Ref#

Got orders and order_products tables.Should total price of order be stored in the orders table, or calculated based on the products quantity and price

So I have the following 3 tables:
Table: Products
Columns: id, name, description, price, currency
Table: Orders
Columns: id, firstName, lastName, phoneNumber
Table: Order_Products
Columns: orderId, productId, quantity
Now I'm trying to figure out where to put the total price of the order and I have 2 ideas:
Add a totalPrice column to the Orders table that will contain the sum of the price * quantity of all products in the order, or:
Add a price column to the Order_Products table that will contain the the price * quantity of that specific product, and then I'd have to get all Order_Products records for that order and sum their price columns.
I'm not quite sure which option is better, hence why I'm asking for recommendations here.
I would recommend that you store the order total in the orders table.
Why? Basically, order totals are not necessarily the same as the sum of all the prices on the items:
The prices might change over time.
The order itself might have discounts.
In addition, the order might have additional charges:
Discounts applied to the entire order.
Taxes.
Delivery charges.
For these reasons, I think it is safer to store financial information on the order when the order is placed.
I woulnd't recommend storing this. This is derived information, that can be computed on the fly whenever needed. If you are going to do the computation often, you can use a view:
create view orders_view as
select o.*, sum(p.price * op.quantity) total_price
from orders o
inner join order_products op on op.orderid = o.id
inner join products p on p.id = op.productid
group by o.id

SQL average and Join

I'm trying to merge these two statements into one query to get the a list of product names(or ids) against the average of their TTFF data, and I'm stuck.
select AVG(TTFF) from TTFFdata group by product_id
select product.product_name, count(*) from product join TTFFdata on product.product_id = TTFFdata.product_id
I've looked into using a temporary table (CREATE TEMPORARY TABLE IF NOT EXISTS averages AS (select AVG(TTFF) from TTFFdata group by product_id)) but couldn't get that to work with a join.
Anyone able to help me please?
You need to understand the components. Your second query is missing a group by. This would seem to be what you want:
select p.product_name, count(t.product_id), avg(t.TTFF)
from product p left join
TTFFdata t
on p.product_id = t.product_id
group by p.product_name
It is better to do group by on product_id, product_name for two reasons. One is, you can select product id along with product name. Second reason is, If the product name is not unique then it may give wrong results(this may be a rare scenario like product name is same but it differs based on other columns like version or model). The below is the final query.
select Product.product_id,
product_name,
AVG(TTFF) as Avg_TTFF
from Product
inner join
TTFFdata
on Product.product_id = TTFFdata.product_id
group by Product.product_id,Product.product_name
TTFFdata:
product:
Output:

mysql - Using Group By

I am trying to write a mysql query for an app I'm developing for android.
I have a database that has a bill_content table and a products table
I want to select top 10 most sold products.
This is a minimal version of what I have, but it's all I need to get an answer here.
bill_content table has the columns: id, id_product, quantity (id_product and quantity here can be duplicate because this table is larger, containing id_bill and other information)
products table has the columns: id, name
SELECT products.name AS Product,
bill_content.quantity AS Quantity
FROM bill_content, products
WHERE bill_content.id = products.id
ORDER BY bill_content.quantity DESC
LIMIT 10
Of course this returns a table of 2 rows containing all the products and their quantity in the bill_content table, but there are duplicates and I need to make sum of their quantity and display them as a single row.
Thank you in advance.
ANSWERED
This could be done using GROUP BY as Gordon Linoff said.
You want a group by. You should also learn to use proper explicit join syntax:
SELECT p.name AS Product,
SUM(bc.quantity) AS Quantity
FROM bill_content bc JOIN
products p
ON bc.id = p.id
GROUP BY p.name
ORDER BY SUM(bc.quantity) DESC
LIMIT 10;

join tables with using max mysql

I have two tables with one to one relationship, the first is "book" and the second is "payment", I want to get the (cus_id) from "book"with the highest (price) from payment using join, which means i have to use max, but i can not get the right syntax for this
book has these columns (cus_id, inv_id as FK,....) and payment has (inv_id as PK, price,...)
I tried this syntax
select b.cus_id, p.price
from customer b, payment p
where b.inv_id=p.inv_id;
but this syntax absolutely won't give me the max price, and here i need the help.
Select a.cus_id,max(price)
from book a,payment
where a.inv_id = (Select b.inv_id from payment b where price = (select max(price) from payment))
select b.cus_id, p.price from customer b, payment p where b.inv_id=p.inv_id AND p.price = (SELECT max(price) FROM payment WHERE inv = p.inv)
if you need only a specific book, add another filter for it