"SQL Must Know and Know" (7, 8): Create calculated fields and use data processing functions

Last time: "SQL must know and know" (4, 5, 6): filter data, advanced data filter, wildcard filter

Lesson 7: Create a calculated field

7.1 Calculation field

The data stored in the database is generally not in the format required by our program, such as:

  • The item order table stores the price and quantity of items, not the total price of each item (you can multiply the price by the quantity). But in order to print the invoice, the total price of the item is required.
  • You need to perform calculations such as totals and averages based on table data.

This is where the calculated field comes in handy. The difference from the previous lessons is that the calculated fields do not indirectly exist in the database table. The calculated field is created in the select statement at runtime.

Field: basically has the same meaning as column, and are often used interchangeably.

Calculated fields, to put it bluntly, cannot be used directly, and need to be processed before being passed to the client.

7.2 Splicing fields

Give a chestnut:
Home address is often not stored in a long list in the database, xx province xx city xx district xxxxx; it involvesTable-level constraints, You can Baidu. Generally, provinces, cities, etc., may not be in the same column in a single table. This is a first-level constraint and the principle of no further division.
But the data we need is the long list. We need to take out the data in the database, splice it, and then send it to the client.

Concatenate: Concatenate values ​​together to form a single value.

select statement, the operator may be used +or ||. But special functions must be used in MySQL.
Insert picture description here
It looks like this in MySQL:

SELECT CONCAT(vend_name,'(',vend_country,')') 
FROM vendors 
ORDER BY vend_name;

Insert picture description hereIf the output data has spaces, you can use the trim() function to remove the spaces.

Use alias

Syntax: as is or direct space and alias are generally acceptable.
as should be supported by all DBMSs.
Insert picture description here
Here is to use spaces directly.

7.3 Perform arithmetic calculations

The most commonly used is the elementary school math problem: always unit price quantity, find the total price.

SELECT prod_id,
quantity ,item_price,
quantity*item_price AS expanded_price
FROM OrderItems
WHERE order_num - 20008 ;

Insert picture description here

Tip: how to test the calculation
The SELECT statement provides a good method for testing, verifying functions and calculations. Although SELECT is usually used to retrieve data from a table, it simply accesses and processes the expression after omitting the FROM clause. For example, SELECT 3*2; will return 6, SELECT Trim(' abc'); will return abc, SELECT Now(); Use the Now() function to return the current date and time. Now that you understand, you can use the SELECT statement to test it as needed.

7.4 Summary

Aliases are very important, can simplify the spelling of our sql words, but also enhance readability. The essence of the database has not changed, just something equivalent to a code name.

Lesson 8: Use data processing functions

8.1 Functions

I can’t get any more familiar with it since I’ve been in junior high school. With now, a function is the entry point of a method, containing a series of low-level operations, and some of them can return the required value by executing the corresponding function. (I said it myself, it may be inaccurate. If you don't like it, don't spray it haha)
The trim() that removes spaces is a function, and its function is to remove spaces.

8.2 Using functions

Most DBMS support the following:

  • A text function for processing text strings (such as deleting or filling values, converting values ​​to uppercase or lowercase)
    .

  • Numerical functions used to perform arithmetic operations on numeric data (such as returning absolute values ​​and performing algebraic operations) .
  • Date and time functions used to process date and time values ​​and extract specific components from these values ​​(such as returning the difference between two dates and checking the validity of the date).
  • System function that returns special information that the DBMS is using (such as returning user login information).

8.2.1 Text processing functions

The most used.
Insert picture description here
Usage reference trim();
Forget it, let's have a chestnut:
upper() to uppercase
Insert picture description here

8.2.2 Date and time processing functions

Date and time are stored in the table using corresponding data types, and each DBMS has its own special form. Date and time values ​​are stored in a special format so that they can be sorted or filtered quickly and efficiently, and save physical storage space.
Applications-generally do not use the date and time storage format, so date and time functions are always used to read, count, and process these values. For this reason, date and time functions play an important role in SQL. Unfortunately, they are very inconsistent and have the worst portability.

SELECT order_num,order_date FROM Orders
WHERE YEAR(order_date) = 2012;

Insert picture description here
The function provided by DBMS is much more than simple date component extraction. Most DBMSs have functions for comparing dates, performing date-based operations, selecting date formats, etc. However, you can see that the date-time processing functions of different DBMSs may be different. For specific DBMS-supported target-time processing functions, please refer to the corresponding documents.

8.2.3 Numerical processing functions

common:
Insert picture description here

8.3 Summary

This lesson introduces how to use SQL data processing functions. Although these functions are very useful in formatting, processing, and filtering data, they are very inconsistent in various SQL implementations and have no portability at all. When using a DBMS, read more help files.

Guess you like

Origin blog.csdn.net/qq_43600467/article/details/113063914