Provides you with an in-depth understanding of MYSQL string connection

Source: Internet
Author: User
MYSQL string connection is not the same as other databases. The following describes the MYSQL string connection method in detail. If you are interested in this, take a look. In Java, C #, and other programming languages, String concatenation can be achieved through the plus sign +, for example, 1 + 3, a + B. In MYSQL, you can also use the plus sign + to connect two strings, for example, the following SQ

MYSQL string connection is not the same as other databases. The following describes the MYSQL string connection method in detail. If you are interested in this, take a look. In Java, C #, and other programming languages, String concatenation can be achieved through the plus sign +, for example, 1 + 3, a + B. In MYSQL, you can also use the plus sign + to connect two strings, for example, the following SQ

MYSQL string connection is not the same as other databases. The following describes the MYSQL string connection method in detail. If you are interested in this, take a look.

In Java, C #, and other programming languages, String concatenation can be achieved by the plus sign "+", for example, "1" + "3", "a" + "B ".
In MYSQL, you can also use the plus sign "+" to connect two strings, such as the following SQL:
SELECT '12' + '33', FAge + '1' FROM T_Employee

Are you surprised to observe the first column? The display result of this column is not the expected "1233", but the "12"
And "33" strings are treated as numbers to calculate the sum of two numbers. Similarly, using the plus sign "+" to connect a number with a MYSQL string is the same effect, for example, the second column here.
In MYSQL, when the plus sign "+" is used to connect two fields (or multiple fields, MYSQL will try to convert the field value to the numeric type (if the conversion fails, the field value is regarded as 0), and then add the field. Therefore, when '12' + '33' is calculated, MYSQL tries to convert the strings '12' and '33' to the numbers '12' and '33, then calculate the value of 12 + 33, which is why we get the result of 45. Similarly, when FAge + '1' is calculated, because FAge is of the numeric type, conversion is not required, while '1' is of the string type, therefore, MYSQL converts '1' to number 1, and then calculates FAge + 1 as the value of the calculated column.
MYSQL will try to convert the field values at both ends of the plus sign to the numeric type. If the conversion fails, the field value is regarded as 0. For example, we execute the following SQL statement:
SELECT 'abc' + '000000', FAge + 'A' FROM T_Employee

The CONCAT function is used to concatenate strings in MYSQL. The CONCAT function supports one or more parameters. The parameter type can be string or non-string, MYSQL will try to convert a non-string type parameter to a string type. The CONCAT function concatenates all parameters into a string in the order of parameters as the return value. For example, the following SQL statement is used to query multiple fields of a user in the form of one calculated field:
Select concat ('employee ID: ', FNumber,' employee happiness index: ', FSalary/(FAge-21 ))
FROM T_Employee

CONCAT supports the usage of only one parameter. In this case, CONCAT can be considered as a function that attempts to convert this parameter value to a string value. MYSQL also provides another CONCAT_WS function for String concatenation. CONCAT_WS can add the specified separator between the strings to be concatenated. Its first parameter value is the delimiter used, the remaining parameters are the string values to be spliced, such as executing the following SQL statement:
SELECT CONCAT_WS (',', FNumber, FAge, FDepartment, FSalary) FROM T_Employee

Different from MYSQL, MSSQLServer can directly use the plus sign "+" to splice strings. For example, execute the following SQL statement:
SELECT 'employee name with employee ID '+ FNumber +' is '+ Fname FROM T_Employee
WHERE FName IS NOT NULL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.