Trigger multiply from two tables - mysql

i need to create a trigger that multiply two fields from two tables, but i have no idea of how to do it, so let's see if you can help me.
Two tables,
Products (product_name, price)
Orders (product_name (foreign key),units_ordered)
I need to add another field on table the Orders that multiply price from Products and units_ordered from Orders, so : total_price = price (from products) X units_ordered (from Orders)
Thanks in advance, and sorry for my bad english.
Regards

You don't need trigger at all. And also you don't need to add another column for the total price as it is already redundant.
If you want their total prices, just do it during the projection of records. Example
SELECT a.Product_Name,
a.Price,
b.units_ordered,
a.Price * b.units_ordered AS TotalPrice
FROM Products a
INNER JOIN Orders b
ON a.Product_name = b.Product_name
or you can create a VIEW out of your SELECT statement. Example,
CREATE VIEW ProductOrder
AS
SELECT a.Product_Name,
a.Price,
b.units_ordered,
a.Price * b.units_ordered AS TotalPrice
FROM Products a
INNER JOIN Orders b
ON a.Product_name = b.Product_name
and selecting from the view,
SELECT * FROM ProductOrder
But if you really want to add another column, still trigger is not an option. You only need to update the values for that column using UPDATE and joining of the two tables. Assuming your new column is called TotalPrice on table `Orders.
UPDATE Orders a
INNER JOIN Products b
ON a.Product_name = b.Product_name
SET a.TotalPrice = a.units_ordered * b.Price

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#

How to query a MySql table with a where clause from another table?

I have two tables in a MySql database, one called sales and one called auctions. The sales table has an auction_id column. I want to extract sales which were only made in EUR, but the currency is a column in the auctions table. What query should I run to achieve this? Many thanks
You can do this using inner query
select *
from sales
where auction_id in (select id from auctions where currency='EUR');
what it will do
first it will run inner query and select all id having currency EUR.
after it will check if sales contain the auction_id which being selected from inner query
Other option is also available that is using join
SELECT *
FROM sales, auctions
WHERE sales.auction_id = auctions.id AND auctions.currency='EUR'
OR
SELECT *
FROM sales
JOIN auctions ON sales.auction_id = auctions.id AND auctions.currency='EUR'

Substract several values from one table to another value in a different table

Hi I am trying to add a column named profit to table invoice. I would like to obtain exactly the values that are in red. That values are the substraction of table invoice amountUsd column MINUS table carrierPayments amountUsd column, using column loadNumber as the ID. It works if I have one carrierPayments per invoice using the following query.
SELECT i.*, i.amountUsd - cp.amountUsd AS PROFIT FROM invoice i INNER JOIN carriersPayments cp ON i.loadNumber = cp.loadNumber
But what if I have more than one carrierPayment with the same loadNumber as the ones with orange background. Could it be possible to make the substraction of more than one carrierPayment to one invoice?
You can use join and group by:
select i.*,
(i.amountUSD - coalesce(p.amountUSD, 0)) as profit
from invoice i left join
(select p.loadNumber, sum(p.amountUSD) as amountUSD
from carrierPayments p
group by p.loadNumber
) p
on p.loadNumber = i.loadNumber;
SELECT i.*, i.amountUsd - sum(cp.amountUsd) AS PROFIT FROM invoice i INNER JOIN carriersPayments cp ON i.loadNumber = cp.loadNumber group by cp.loadNumber

Mysql join tables but only results with a specific row

three tables:
Customers (id_cus, cli_name, ...)
Products (id_pro, pro_name, ...)
Orders (id_cus, id_pro)
Table Orders is relation between Customers and Products.
Question is: How to get Customers (id_cus) who choose only specific product.
Example: Product A (id_pro= 100) and want all Clients customers who only bought this Product A, not Product A and Product B. Only Product A (id_pro = 100).
SELECT `id_cus` from `Orders` where `id_pro=100;
This give me all Customers who have ever bought Product A (and maybe Product B, C too).
Sorry, no idea.
Take a look to EXISTS
Simply:
SELECT o.id_cus
FROM orders o
WHERE o.id_pro = 100
AND NOT EXISTS (SELECT * from orders o2 where o2.id_cus = o.id_cus AND o2.id_pro != 100)
You can use aggregation for this:
select o.id_cus
from orders o
group by o.id_cus
having min(o.id_pro) = max(o.id_pro) and min(o.id_pro) = 100;

SQL Join, right ? left ? inner?

working with mySql I would like to list all purchases that customers made on a specific cathegory of products.
So, I had 3 tables: customers (idCustomer, Name) , cathegories (idCategory, CategoryName) and orders (idOrder, idCustomer, idCathegory, Qty, Price)
But I want a listing with ALL of the customers.
Not only the one who bought that specific idCategory
I thought something like:
select sum(Orders.Qty), Customers.Name
from Orders
right join Customers on Orders.idCustomer = Customer.idCustomer
where Orders.idCategory = 'Notebooks'
group by Orders.idCategory
but this statement only lists the records for customers who exists in Orders table.
And I want all of them ( the one who didnt buy, with qty =0 )
thanks in advance
Most people find left join easier to follow than right join. The logic for left join is to keep all rows in the first table, plus additional information from the remaining tables. So, if you want all customers, then that should be the first table.
You will then have a condition on the second table. Conditions on all but the first table should be in the on clause rather than a where. The reason is simple: when there is no match, then the value will be NULL and the where condition will fail.
So, try something like this:
select sum(o.Qty) as sumqty, c.Name
from Customers c left join
Orders o
on o.idCustomer = c.idCustomer and
o.idCategory = 'Notebooks'
group by c.Name;
Finally, the group by should have a relationship to the select clause.
Try this query
select sum(Orders.Qty), Customers.Name
from Customers
right join Orders on Customer.idCustomer = Orders.idCustomer and Orders.idCategory = 'Notebooks'
group by Customers.Name