📜  mysql select where text contains - SQL (1)

📅  最后修改于: 2023-12-03 15:17:46.190000             🧑  作者: Mango

MySQL SELECT WHERE Text Contains - SQL

MySQL SELECT WHERE Text Contains is a powerful feature to search for specific text within a column in a MySQL database. This feature is used to retrieve only the rows that contain the specified text.

Syntax

The syntax for MySQL SELECT WHERE Text Contains is:

SELECT * FROM table_name WHERE column_name LIKE '%search_text%';
  • SELECT: Selects the columns to retrieve data from the table.
  • FROM: Specifies the table to retrieve data from.
  • WHERE: Filters the rows to retrieve based on a specified condition.
  • column_name: Specifies the column to search the text for.
  • LIKE: Specifies a pattern to match the text against.
  • %: Matches any number of characters within the text.
Examples

Consider the following table employees:

| id | name | email | designation | |----|------|-------|-------------| | 1 | John | john@example.com | Developer | | 2 | Jane | jane@example.com | Designer | | 3 | Mark | mark@example.com | Developer | | 4 | Lisa | lisa@example.com | Manager |

Example 1

To retrieve all the rows where the name column contains the text 'John':

SELECT * FROM employees WHERE name LIKE '%John%';

This will return the following result:

| id | name | email | designation | |----|------|-------|-------------| | 1 | John | john@example.com | Developer |

Example 2

To retrieve all the rows where the email column contains the text 'example.com':

SELECT * FROM employees WHERE email LIKE '%example.com%';

This will return the following result:

| id | name | email | designation | |----|------|-------|-------------| | 1 | John | john@example.com | Developer | | 2 | Jane | jane@example.com | Designer | | 3 | Mark | mark@example.com | Developer | | 4 | Lisa | lisa@example.com | Manager |

Example 3

To retrieve all the rows where the designation column contains the text 'Developer':

SELECT * FROM employees WHERE designation LIKE '%Developer%';

This will return the following result:

| id | name | email | designation | |----|------|-------|-------------| | 1 | John | john@example.com | Developer | | 3 | Mark | mark@example.com | Developer |

Conclusion

MySQL SELECT WHERE Text Contains is a useful feature to retrieve only the specific rows that contain the specified text. It can be used in various scenarios where specific text needs to be searched within a large database.