How to get better search results in MySQL

Source: Internet
Author: User

MySQL databaseA built-in full-text search solution is provided to address this problem. Here, developers only need to simply mark the fields that require full-text search, and then use special MySQL methods to run search on those fields, this not only improves the performance and efficiency, because the MySQL database indexes these fields to optimize the search), but also achieves higher quality search, because MySQL uses natural language to intelligently rate the results to remove irrelevant items.

This article describes how to perform full-text search in MySQL.

1. Set the basic table

Use the following SQL command to create an example table:

Reference content is as follows:
Mysql> create table reviews (id INT (5)
Primary key not null AUTO_INCREMENT, data TEXT );
 

The above command creates a simple music album database that mainly contains text in the entire segment), and then adds some records to the table:

Reference content is as follows:
Mysql> insert into 'Reviews '('id', 'data') VALUES
(1, 'gingerboy has a new single out called Throwing Rocks.
It \'s great! '); Mysql> insert into 'Reviews' ('id', 'data ')
VALUES (2, 'Hello all, I really like the new Madonna single.
One of the hottest tracks currently playing...
I \ 've been listening to it all day'); mysql> INSERT INTO 'review'
('Id', 'data') VALUES (3, 'Have you heard the new band Hotter
Than Hell? They have five members and they burn their
Instruments when they play in concerts.
These guys totally rock! Like, awesome, dude! ');
 

Verify that the data is entered correctly:

Reference content is as follows:
Mysql> SELECT * FROM reviews;
+ ---- + -------------------------------------------- +
| Id | data |
+ ---- + -------------------------------------------- +
| 1 | Gingerboy has a new single out called... |
| 2 | Hello all, I really like the new Madon... |
| 3 | Have you heard the new band Hotter Than... |
3 rows in set (0.00 sec)
2. Define full-text search fields

Next, define the fields to be indexed as full-text search

Reference content is as follows:
Mysql> alter table reviews add fulltext index (data );
Query OK, 3 rows affected (0.21 sec)
Records: 3 Duplicates: 0 Warnings: 0
Run the show indexes command to check whether the index has been added:

Reference content is as follows:
Mysql> show indexes from reviews;
+ --------- + --------------- + -------- + ------ + ------------ + --------- +
| Table | Column_name | Packed | Null | Index_type | Comment |
---------- + --------------- + -------- + ------ + ------------ + --------- +
| Reviews | id | NULL | BTREE |
Reviews | data | NULL | YES | FULLTEXT |
+ --------- + --------------- + -------- + ------ + ------------ + --------- +
Searching in a MySQL database is one of the most important functions in a MySQL database. using full-text search brings great convenience to database management.

Related Article

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.