简体   繁体   中英

Creating a MYSQL view from equations

Using MYSQL, I need some help creating a view from multiple tables and performing some calculations.

My problem is that the equation adds extra decimal places to my value, is there a way to proceed and avoid those extra decimal places? Here is my SQL statement for the view

    CREATE ALGORITHM=UNDEFINED DEFINER=`Tosby`@`%` SQL SECURITY DEFINER VIEW `view_products`  AS  select `tbl_products`.`id` AS 
`id`,`tbl_products`.`name` AS `name`,`tbl_products`.`descr` AS `descr`,`tbl_products`.`image` AS `image`,`tbl_categories`.`name` AS 
`category`,((`tbl_products`.`cost` * (`tbl_tax`.`amount` / 100)) + `tbl_products`.`cost`) AS `cost`,
`tbl_restaurants`.`name` AS `restaurant` from (((`tbl_products` join `tbl_categories` on((`tbl_products`.`category` = `tbl_categories`.`id`))) 
join `tbl_tax` on((`tbl_products`.`tax` = `tbl_tax`.`id`))) 
join `tbl_restaurants` on((`tbl_products`.`owner` = `tbl_restaurants`.`id`))) ;

The line giving the problem is this

    ((`tbl_products`.`cost` * (`tbl_tax`.`amount` / 100)) + `tbl_products`.`cost`) AS `cost`,

How could I remove the extra decimal places added in the length of the field? Note that all those fields used in the equation have integers as the datatype and the length is 4 characters.

use round()

SELECT ROUND(135.37589567657, 2); it will return 135.38

so in you case

round (((`tbl_products`.`cost` * (`tbl_tax`.`amount` / 100)) + `tbl_products`.`cost`),n)

Where n = the number of decimal places you want to have

use format()

reference link

SELECT FORMAT(12324.2573, 3); //output 12324.257

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM