MySQL automatically updating field when inserting data

Bas

I'm making a web application to make customers order items for anything. For that I've made a MySQL database which has the following tables:

  • customers
  • orders
  • orders-items
  • products

In the customers table is all the information about the person such as:

  • The customer ID, for the primary key and auto increment (id)
  • The first name (first_name)
  • The last name (last_name)
  • The email address (email_address)
  • Information about the customer (customer_info)

Example:

enter image description here

In the orders table is all the specific information about it such as:

  • The order ID, for the primary key and auto increment (id)
  • Which customer it ordered, linked with id field from the customers table (customer_id)
  • Order information (order_info)
  • The location where the order needs to go to (location)
  • The total price the customer has to pay (total_price)
  • When the order was created (created)

Example:

enter image description here

In the orders-items table are all the items which every customer ordered, this is being linked by the order-id from the previous table.

  • The ID, for primary key and auto increment, not used for any relation (id)
  • The order ID, used for which product is for which order. This is linked with the id field from the orders table (order_id)
  • The product ID, this is used for what product they ordered, this is linked with the id field from the products table. (product_id)
  • The amount of this product they ordered (quantity)

Example:

enter image description here

In the products table is all the information about the products:

  • The ID, for primary key and auto incrementing, This is linked with the product_id field from the order_items table (id)
  • The name of the product (name)
  • The description of the product (description)
  • The price of the product (price)

Example:

enter image description here

Question:

I've got this query:

SELECT `orders-items`.`order_id` , SUM(`orders-items`.`quantity`* `products`.`price`) total
FROM  `orders-items` 
INNER JOIN  `Products` ON  `orders-items`.`products_id` =  `products`.`id` 

And it shows me a list of all the total prices every order_id has to pay.

But how do I make this so that this value of the total_price every order_id has to pay is automatticly inserted into the orders table inside the total_price field at the right order_id when inserting a product into my orders-list table?

Or is it still better to not keep track of the total_prices the customers have to pay?

Havenard

A couple things to consider.

Having a total_price for itself is redundant. You can learn this total by summing the prices of this order's items at any time. It might be interesting to have it for performance reasons, but is this really necessary for your scenario? It rarely is.

Having a price on each order_item in the other hand would be useful. And the why is because thoses products prices might change in the future and you don't want to lose information of for how much they were sold at the time of that particular sale.

In any case, you can update your total_price using triggers like this:

DELIMITER $$

CREATE TRIGGER order_items_insert AFTER INSERT ON `orders-items` FOR EACH ROW
BEGIN
    UPDATE orders o INNER JOIN (SELECT i.order_id id, SUM(i.quantity * p.price) total_price FROM `orders-items` i INNER JOIN products p ON p.id = i.products_id AND i.order_id = new.order_id) t ON t.id = o.id SET o.total_price = t.total_price;
END$$

CREATE TRIGGER order_items_update AFTER UPDATE ON `orders-items` FOR EACH ROW
BEGIN
    UPDATE orders o INNER JOIN (SELECT i.order_id id, SUM(i.quantity * p.price) total_price FROM `orders-items` i INNER JOIN products p ON p.id = i.products_id AND i.order_id = new.order_id) t ON t.id = o.id SET o.total_price = t.total_price;
END$$

CREATE TRIGGER order_items_delete AFTER DELETE ON `orders-items` FOR EACH ROW
BEGIN
    UPDATE orders o INNER JOIN (SELECT i.order_id id, SUM(i.quantity * p.price) total_price FROM `orders-items` i INNER JOIN products p ON p.id = i.products_id AND i.order_id = old.order_id) t ON t.id = o.id SET o.total_price = t.total_price;
END$$

DELIMITER ;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL automatically updating field when inserting data

From Dev

Automatically Initialize GETDATE() when inserting into MYSQL

From Dev

Updating data in MySQL database after inserting in the wrong encoding

From Dev

Updating field value on MySQL table after 24 hours automatically

From Dev

PHP MYSQL ERROR when inserting data

From Dev

500 Internal Server when Inserting data to MySQL

From Dev

Splitting data field into two columns & Inserting it to an existing table (MYSQL)

From Dev

Error while inserting values into a MySQL multipolygon data type field

From Dev

MySQL trigger updating field

From Dev

Not inserting one field in mysql query

From Dev

Can INSERT when UPDATING but not when INSERTING

From Dev

Inserting data with MySQL

From Dev

MySQL data not inserting into database

From Dev

ERROR: Inserting data into mySQL

From Dev

Inserting Data into the MySQL Database

From Dev

MySql Database inserting data

From Dev

Inserting a default data into MySql

From Dev

Data not inserting in MySQL

From Dev

Data not inserting into MySQL database

From Dev

ERROR: Inserting data into mySQL

From Dev

Inserting data in Mysql database

From Dev

Inserting JSON data into MySQL

From Dev

0 value when inserting data into mysql table using php

From Dev

MySQL use GROUP_CONCAT when INSERTing data

From Dev

Error when inserting data in mysql using php insert.php

From Dev

Changing a field in MYSQL automatically

From Dev

selectively UPDATING old rows when INSERTING new ones into table (trigger issue) in MySQL

From Dev

Time stamp automatically update when update any field in mysql

From Dev

Mysql Inserting Or Updating one table column data to another table with Matching valus

Related Related

HotTag

Archive