首页 > 解决方案 > 从方程式创建 MYSQL 视图

问题描述

使用 MYSQL,我需要一些帮助来从多个表创建视图并执行一些计算。

我的问题是该等式为我的值增加了额外的小数位,有没有办法继续并避免这些额外的小数位?这是我的视图的 SQL 语句

    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`))) ;

给出问题的行是这个

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

如何删除字段长度中添加的额外小数位?请注意,方程式中使用的所有这些字段都将整数作为数据类型,长度为 4 个字符。

标签: mysqlsqlview

解决方案


采用round()

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

所以在你的情况下

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

其中 n = 您想要的小数位数


推荐阅读