MySQL Join to show all rows - mysql

I have two MySQL tables with the following structure:
TABLE `orders`
order_id
order_date
product_id
TABLE `products`
product_id
product_name
product_price
product_type
I want to show all rows from table orders and if there is a product_id in TABLE products show all data of that product. If there isn't, show all data from orders table only.
I tried this:
SELECT * FROM orders AS o RIGHT JOIN products AS p ON o.product_id=p.product_id
But if we have a row with a product_id inside orders table that doesn't exist in products table, then the row from orders is not showed. I want the row from orders id to show no matter if there is a product_id inside products table. If there is JOIN it, if it isnt show just the data from table orders.
Any suggestions?

You should be doing a LEFT JOIN
With a left join, all rows from ORDERS will be returned. The columns from the products table will be null if the criteria for the join is not met.
SELECT o.*,p.*
FROM orders AS o
LEFT JOIN products AS p
ON o.product_id=p.product_id`
See the MySQL manual join syntax

Related

How to get price from second table if data exists else from first table?

I have two tables
2)- First Table where I store all product details like name, price, weight, etc.
1)- Second Table where I store updated price with product_id
I want to fetch an updated price with product_id from the second table but if the product_id is not available on the second table then the price will come from first table.
Left join and coalesce
select t1.pid,name, coalesce(t2.price,t1.price), weight
from t1
left join t2 on t1.pid = t2.pid;

Creating a percentage column on a table made from an inner join

I have two tables Orders and RMA. I wrote this command to return an inner join between the two tables. OrderID is the primary key of Orders and foreign key of RMA.
SELECT Orders.SKU, COUNT(*) AS Frequency
FROM Orders
INNER JOIN RMA ON Orders.OrderID = RMA.OrderID
GROUP BY Orders.SKU
ORDER BY COUNT(*) DESC;
This select statement returns a table with one column containing SKU values and one column containing the number of times each SKU value appears in the data. My goal is to create a third column that includes a percent that represents the frequency of each SKU value.
(disclaimer: I'm new to mysql, so if there's more information needed for this question, I am happy to provide it. Thanks!)
You must divide COUNT(*) with the total number of rows in RMA:
SELECT Orders.SKU,
COUNT(*) AS Frequency,
COUNT(*) / (SELECT COUNT(*) FROM RMA) AS percent
FROM Orders INNER JOIN RMA
ON Orders.OrderID = RMA.OrderID
GROUP BY Orders.SKU
ORDER BY Frequency DESC;

Use mysql joins to count no of invoices of customers

My invoices table has orderId column which is a foreign key referencing orderId primary key column of orders table and orders table has customerId column which references to customerId primary key column of customers table.
A customer can have multiple orders but an order has only one invoice.
I want to count the no of invoices of each customer. Below is the query I tried:
SELECT customers.name, COUNT(*) as number_of_invoices
FROM invoices
JOIN orders
ON invoices.orderId = orders.orderId
JOIN customers
ON orders.customerId = customers.customerId;
But it is only returning me one customer and the count is of total count not invoices count of that customer.
Here is a solution for your problem:
SELECT customers.name, COUNT(*) as number_of_invoices
FROM invoices
INNER JOIN orders
ON invoices.orderId = orders.orderId
INNER JOIN customers
ON orders.customerId = customers.customerId
GROUP BY Customers.name;
Just add GROUP BY in your query in last to group count of invoices for each Customer.
If you just need to know the count of invoice for each customer. It can be done without JOIN. Since order and invoice has one-to-one mapping you can use below sql queries
select customerId ,count(orderId) as invoice_count from orders group by customerId

SQL: How select rows and minus from another table

how construct sql query to select rows from table "Product" and minus sum(pc) from another table "Order".
Here is schema.
Thanks!
Group your orders by the product to get the sum of the pc-column for the products.
Then join the sum onto your product table, subtract the sum of the orders from your pc-column in the product table.
SELECT
product.id_product,
(product.pc - ifnull(orders.amount, 0))
FROM
product
LEFT JOIN (
SELECT
id_product,
SUM(pc) AS amount
FROM
`Order`
GROUP BY
id_product
) AS orders ON orders.id_product = product.id_product

selecting complex query into an exsiting column

I have a table, one column of which is VERY dependant on a bunch of other tables
i can't alter the table, and i can't change the name of the parameter that is used
this is my query :
select *,(select sum(itemQuantity*(select itemPrice from Items where
Items.itemID=OrderItems.itemID)) from OrderItems
where OrderItems.orderNumber=Orders.orderNumber) as orderValue,
(select sum(itemQuantity) from OrderItems where OrderItems.orderNumber=Orders.orderNumber)
as orderItemQuantity from Orders WHERE Orders.customerId =1 AND Orders.beenSupplied =1
and this is the result of this query:
as you can see, i have the column "orderValue" twice
the first orderValue is the original column from the Orders table, and the second orderValue is from the "as" clause in the query
how do i merge the two columns and have the output of the query in it, and not the 0 that constantly gets inserted ?
EDIT:
table structures:
Rather than using SELECT *, explicitly list the columns (from your Orders table) that you wish to select.
You can also avoid using (the highly inefficient) correlated subqueries by joining the tables in the outermost query and then grouping each order.
SELECT Orders.orderNumber,
Orders.customerId,
Orders.orderDate,
Orders.beenSupplied,
Orders.purchaseDate,
SUM(OrderItems.itemQuantity * Items.itemPrice) AS orderValue,
SUM(OrderItems.itemQuantity) AS orderItemQuantity
FROM Orders
JOIN OrderItems USING (orderNumber)
JOIN Items USING (itemID)
WHERE Orders.customerId = 1
AND Orders.beenSupplied = 1
GROUP BY Orders.orderNumber