mysql must know will be - create calculated fields

 Calculated Field

Data stored in a database table format application program is generally not required. Below cite
a few examples.
* If you want to both show the company name in a field, but also shows the company's address, but both
a general information contained in different columns of the table.
* City, state and zip code are stored in separate columns (should be so), but the message label
printing program, but they need to be a proper format as fields retrieved.
* Column data is mixed case, but the program requires reporting all data expressed as a capital
out.
* Price and the number of items stored in the Orders table items, but always need to store each item's
price (price times quantity can be used). Print invoices, the total price of items needed.
* The total number of required data table, or other computing average calculation.
In each of these examples, data is stored in the table is not required for the application.
We need to directly retrieved from the database conversion, calculated or formatted data; instead
retrieve the data and then reformat the client application or reporting procedures.
This is a calculated field where the play. And differs from that described in the previous chapters through the column,
the calculated field does not actually exist in the database table. Calculated field is running in the SELECT statement
created within.

Field (field) is substantially the same as the row (column) mean, often interchangeably
used, but is generally referred to as column database column, and the term commonly used in the field of computed field
connection.
It is important to note that only the database know which columns in the SELECT statement is the actual
table column, which columns are calculated fields. From the client (e.g., application) perspective, calculated
data field is same as the data in a manner other columns returned.

Format the client and server can be completed within a SQL statement many conversion
and formatting work can be done directly within the client application. But a
general, perform these operations on the database server than done in the client
into a much faster, because the DBMS is designed to quickly and efficiently complete this at
reasonable.

Splicing field

Splicing (CONCATENATE) coupled together to form a single value of values.
The solution is to splice together two columns. In MySQL SELECT statement can be used
Concat () function to splice two columns

Most DBMS MySQL except || implemented using the + or stitching,
MySQL use Concat () function to implement. When converted into SQL statements
when MySQL statement must take this distinction in mind.

Concat () splice sequence, i.e. the plurality of strings are connected to form a long string.
Concat () requires one or more specific strings, between the respective string separated by commas.
The above SELECT statement connecting the following four elements:

  • Vend_name name stored in the column;
  • Comprising a space and a left parenthesis string;
  • State is stored in vend_country column;
  • String comprising a closing parenthesis.
    Can be seen from the above output, SELECT statement returns a single column containing the above-described four elements
    (calculated field).

To organize data by deleting data extra spaces on the right side, which can be
used in MySQL RTrim () function to complete, as follows:

In addition to supporting RTrim MySQL Trim function () (As just seen, which removes the
space to the right of the string), supports the LTrim () (to remove the string spaces left) and
Trim () (right and left spaces on both sides of the string removed).

Using aliases

We can see from the preceding output, SELECT statements address field splicing work very well.
But what does this new calculated column name is it? In fact, it has no name, it's just a value. As
if only look at the results of an SQL query tool, so nothing bad. However, a non-
named columns can not be used for client applications, because the client is no way to refer to it.
To solve this problem, SQL supports column aliases. Alias (Alias) is a field value or
replacement name. Alias given by the AS keyword. Consider the following SELECT statement

SELECT statement itself is the same as the previously used only in the calculation statement here
with text AS vend_title after field. It indicates SQL Create a
named vend_title computing field specifies the calculation. We can see from the output, with the result to
the front is the same, but now the column named vend_title, any client application can be referenced by name
in this column, as if it were an actual column of the same table

Other uses aliases have other uses aliases. Common uses include the actual
renaming it at the table column name contains illegal characters (such as spaces), the
expansion of its original name ambiguous or misunderstood when, etc.

Export column aliases are also sometimes referred to as export column (derived column), no matter referred to
what they represent is the same thing

Perform arithmetic calculations

Another common use of calculated field is retrieved data arithmetically calculated

How to calculate the SELECT test provides a test and function test and calculation of
very good way. While the SELECT typically used to retrieve data from the table, but may
be omitted for simple access to the FROM clause expression and processing. For example, the SELECT
. 3 * 2; returns 6, SELECT Trim ( 'abc' ); returns abc, and the SELECT
Now () using Now () function returns the current date and time. These examples
will be understood how SELECT tested as required.

This chapter describes how to create calculated fields and calculated fields. We count the examples
use count field stitching and string arithmetic calculations. In addition, you learn how to create and use the other
name, so that applications can reference calculated fields.

Guess you like

Origin www.cnblogs.com/ygjzs/p/12229929.html