mysql - 从方程式创建 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 个字符。
解决方案
采用round()
SELECT ROUND(135.37589567657, 2); it will return 135.38
所以在你的情况下
round (((`tbl_products`.`cost` * (`tbl_tax`.`amount` / 100)) + `tbl_products`.`cost`),n)
其中 n = 您想要的小数位数
推荐阅读
- python - Pytorch 使用 HTTPS 加载自定义数据非常慢
- python - 如何在 Kivy 中获取 AnchorLayout 的更新位置?
- vert.x - UDP 多播是否适用于 VertX 3 中 0.0.0.0 以外的地址
- java - 为什么Java流API中的某些收集器称为下游收集器?
- ios - 检查 UITextField 是否仅为字符/数字
- wpf - WPF PrintPreview 获取 DocumentPaginator 页面的 PageContent
- python-3.x - Tensorflow-gpu 没有检测到 GPU?
- node.js - nodejs:子进程不执行 process.on('message')
- node.js - 我的 graphql 服务器查询返回空值
- asp.net-core - 使用 Azure AD 登录并获取 UserManager