当前位置:网站首页>MySQL Leak Detection and Filling (3) Calculated Fields
MySQL Leak Detection and Filling (3) Calculated Fields
2022-08-09 09:10:00 【Hu Letian】
Create Calculated Field
The data stored in the database table is not in the format required by general applications. At this time, we need to perform calculations to obtain the desired data format.
Example:
If you want to display both the company name and the company address in one field, but the two information are contained in separate table columns.
In the above example, the data stored in the table is not required by the application, we need to retrieve the transformed, calculated or formatted data directly from the database, rather than retrieving the data, and then inReformatting in the client application or reporter.
This is where calculated fields come into play.Calculated fields do not exist directly in the database table.Calculated fields are created within a SELECT statement at runtime.
Term Explanation (Field)
A field basically means the same thing as a column and is often used interchangeably, although database columns are generally referred to as columns, and the term field is often used in conjunction with computed fields.
Note:
Only the database knows which columns in the SELECT statement are actual table columns and which columns are computed fields.From the perspective of a client (such as an application), the data for a calculated field is returned in the same way as data for other columns.
First, splicing fields
The so-called concatenation is the connection, which connects two or more fields to form a single value.MySQL uses the concat() function to implement, and most other DBMSs use + or || to implement.The connection fields in Oracle can be implemented using concat() or ||.
select concat(company_name,',',company_address) from company_info
Analysis:
What this statement needs to query is the connection field in the company information table. The connection field is: company name + comma + company address.
Note:
If you want to remove the spaces on both sides of the table column and connect, you can use the trim() function.If you only want to remove the spaces on the right, use the function Rtrim(), and if you only want to remove the spaces on the left, use the function Ltrim().
What is the name of the concatenated field?In fact, he does not have a name. At this time, if we want to obtain it in the application, it will be time-consuming. At this time, we can assign an alias to him.
For example, assign an alias to the concatenation fields of the company name and company address above: companyInfo
select concat(company_name,',',company_address) as companyInfo from company_infoselect concat(company_name,',',company_address) companyInfo from company_info
The above two SQL statements achieve the same effect, except that the second statement omits the keyword as.This way, any client application can use companyInfo to refer to this column, just like an actual table column.
Second, arithmetic calculation
Another common use of calculation fields is to perform arithmetic calculations on retrieved data, that is, addition, subtraction, multiplication and division.
Example:
For example, a product shopping record table, the columns of the table contain the order number, product name, product unit price, and product quantity. Now you want to query the product purchased with the order number (201542) and the amount spent on the product.
select item_name,item_number*item_price as totalPricefrom order_infowhere order_number= '201542'
MySQL supports basic arithmetic operators, namely addition, subtraction, multiplication and division (±*/). In addition, parentheses can be used to distinguish the order of precedence.Generally in application development, if the priority of basic operations is involved, it is recommended to use parentheses for programming, so that it is clear and the error rate is small.
边栏推荐
- on duplicate key update
- 不支持关键字: 'Provider'
- 【培训课程专用】CA/TA调用模型-代码导读
- ARMv8/ARMv9视频课程-Trustzone/TEE/安全视频课程
- [漏洞复现]CVE-2018-7490(路径遍历)
- ASP.net中的数据库应用
- [Vulnerability reproduction] CVE-2018-12613 (remote file inclusion)
- Failed to mount component: template or render function not defined.
- STM32 如何知道FLASH的使用情况
- XCTF College War "Epidemic" Network Security Sharing Competition Misc wp
猜你喜欢
随机推荐
QT设置exe可执行文件的图标
uniapp编译到小程序后丢失static文件夹问题
Calendar类和Date类转换时区 && 部分时区城市列表
Makefile中的%标记和系统通配符*的区别
BUUCTF MISC刷题笔记(一)
STM32 如何知道FLASH的使用情况
VoLTE基础自学系列 | IMS的业务触发机制
gin中简单的curd接口例子
MySQL查漏补缺(二)排序检索、过滤数据、模糊查询、正则表达式
不支持关键字: 'Provider'
epoll LT和ET 问题总结
makefile的foreach、filter、filter-out函数
Anaconda4.8.3介绍、安装及使用教程安装(win10)并修改Jupyter默认工作目录
SQL Server2000 各个版本之间的区别
加密技术和电子竞技如何促进彼此的发展
【培训课程专用】CA/TA调用模型-代码导读
SQL语言中的distinct说明
elder blind date
政务中心导航定位系统,让高效率办事成为可能
Getting started with ctfshow-web Part of the file upload part solution