select multiple rows from tow tables by duplicates of one table - mysql

I want to list bestsellers
I have list of duplicates of sold products from table SALE and want to select those product properties from PRODUCT table
my query to find duplicates from table SALE is:
SELECT product, COUNT(*) AS COUNT
FROM sale
GROUP BY product
HAVING COUNT(*) > 1
ORDER BY COUNT DESC
this query lists my bestsellers products, how to list these products properties from PRODUCT table in this order

In MySQL you can just join in the product table:
SELECT p.product, COUNT(*) AS COUNT
FROM product p JOIN
sale s
ON p.product = s.product
GROUP BY p.product
HAVING COUNT(*) > 1
ORDER BY COUNT DESC ;
This assumes that p.product is the primary key on PRODUCT that links it to SALES. Also, are you just counting "orders" or do you have a "quantity" field that you also care about?

Related

How to count grouped records that match multiple criteria

i have three tables
table 1; product table with product_name, product_id, product price
table 2; store table with store_id, store_name
table 3; stock table with id, store_id, product_id, sock, min_stock
i need to sum the stock of each product as total_stock and also sum the min_stock of each product as total_min_stock.
I should then count the number of products whose total_stock value is less than total_min_stock as low_stock_number and echo the value of low_stock_number
Assuming that each table's unique value is its id, what you need is to aum and group by, using HAVING to condition the GROUPed results
SELECT
p.product_id,
SUM(IFNULL(st.stock,0) as total_stock,
SUM(IFNULL(st.min_stock,0) as total_min_stock
FROM
product p
LEFT JOIN stock st ON st.product_id = st.product_id
GROUP BY
p.product_id
HAVING
total_stock < total_min_stock

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

Join creates duplicate rows

I'm trying to join 3 tables in BigQuery that contain sales for products with the same identifier (SKU) and attending to the order date.
My goal would be to have in one table the order date, Sales Table 1, Sales Table 2, Sales Table 3 and Product identifier (SKU). For that I've been trying the next query:
SELECT
Order_Date, Sales1, Sales2, Sales3, SKU
FROM
Table 1
LEFT JOIN
Table 2 ON Order_Date_Table1 = Order_Date_Table2 AND SKU_Table1 = SKU_Table2
LEFT JOIN
Table 3 ON Order_Date_Table1 = Order_Date_Table3 AND SKU_Table1 = SKU_Table3
However when 2 of the tables contain sales for the same SKU and the same day, I get duplicated rows. Something similar happens with all other join types.
How can I avoid this?
It seems to me that aggregation can be a solution for your problem:
SELECT Order_Date, SKU, SUM(Sales1), SUM(Sales2), SUM(Sales3)
...
GROUP BY Order_Date, SKU

count rows in one table based on the id's of the other table

I have two tables:
Orders
Orders product
I currently have the query that counts the amounts of orders:
SELECT COUNT(*) as total FROM orders WHERE DATE(`created_on`) = CURDATE()
This gives me the amount of orders from today
Now I want to change this query, so insted of that I count the orders I count the amount of products that they ordered.
The order_products table is like this:
-id
-order_id
-product_name
-product_weight
etc etc
I am only interested in the order_id because that links this table to the orders table. For every product that is ordered, 1 row is added in this table linked to the order with the order_id.
Is it possible to have one single query to select the count of that, or is that not possible? I think I have to use a LEFT JOIN for this operation, but I cannot seem to find it.
You just need to create inner join between those table and applying the relation throw foreign key then you need to add group by condition on what you are looking for
SELECT COUNT(*) as total FROM orders o inner join order_product op on(op.order_id=o.order_id) WHERE DATE(`created_on`) = CURDATE() group by op.id

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;