Home  >  Article  >  Database  >  How to use the LEFT function in MySQL to intercept the left part of a string

How to use the LEFT function in MySQL to intercept the left part of a string

PHPz
PHPzOriginal
2023-07-12 13:37:391288browse

How to use the LEFT function in MySQL to intercept the left part of a string

In database management systems, we often encounter situations where we need to intercept a certain part from a string. MySQL provides many built-in string functions, including the LEFT function, which can be used to intercept the left part of a string.

The syntax of the LEFT function is as follows:

LEFT(str, length)

Among them, str is the string to be intercepted, and length is the length to be intercepted.

Next, we will demonstrate how to use the LEFT function through code examples.

For example, we have a table named "employees", which has a field named "full_name" that stores the full name of the employee. Now we need to extract the employee's last name from the "full_name" field.

First, we need to connect to the MySQL server and select the corresponding database. We can then use the following code to create a table called "employees" and insert some sample data.

CREATE TABLE employees (

id INT PRIMARY KEY AUTO_INCREMENT,
full_name VARCHAR(50)

);

INSERT INTO employees (full_name) VALUES

('Tom Smith'),
('John Doe'),
('Emma Johnson');

Next, we can use the following code to query "employees " table, and use the LEFT function to intercept the left part of the last name.

SELECT LEFT(full_name, LOCATE(' ', full_name) - 1) AS last_name FROM employees;

In the above code, the LOCATE function is used to find the position of the first space. The first parameter of the LEFT function is the string to be intercepted, and the second parameter is the length to be intercepted. By subtracting 1 from the return value of the LOCATE function as the length of the truncation, we can get the left part of the last name.

After running the above code, the query results are as follows:

##Tom JohnEmma
last_name
Through the above example, we can see How to use the LEFT function in MySQL to intercept the left part of a string. In practical applications, the flexibility of the LEFT function allows us to intercept different parts of the string according to specific needs to meet various data processing needs.

Summary:

This article introduces how to use the LEFT function in MySQL to intercept the left part of the string. Through code examples, we demonstrate how to create a sample table and insert data, as well as how to use the LEFT function for query operations. It is hoped that these examples can help readers better understand and apply the LEFT function, thereby improving the efficiency and accuracy of database operations.

The above is the detailed content of How to use the LEFT function in MySQL to intercept the left part of a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn