Get the App
SLTechnology News&Howtos  ›  Database  › 

How to get better search results in MySQL

Shulou Source: shulou.com Published: 2022-05-31 11:35:27 10月06日 Update

This article mainly shows you "how to get better search results in MySQL". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to get better search results in MySQL".

1. Set up the basic form

To start by creating an example table, use the following SQL command

The following is the referenced content:

Mysql > CREATETABLEreviews (idINT (5)

PRIMARYKEYNOTNULLAUTO_INCREMENT,dataTEXT)

The above command creates a simple database of music albums (mainly entire paragraphs of text), and then adds some records to the table:

The following is the referenced content:

Mysql > INSERTINTO`roomws` (`id`, `data`) VALUES

(1) GingerboyhasanewsingleoutcalledThrowingRocks.

It\ 'skeeper'); mysql > INSERTINTO`roomws` (`id`, `data`)

VALUES (2) Helloall1.

Oneofthehottesttrackscurrentlyplaying...

I\ 'vebeenlisteningtoitallday'); mysql > INSERTINTO`roomws`

(`id`, `data`) VALUES (3 recorder HaveyouheardthenewbandHotter

ThanHellTheyhavefivemembersandtheyburntheir

Instrumentswhentheyplayinconcerts.

Theseguystotallyrocklike Like, awesome, and dudewings')

Verify the correct entry of data:

The following is the referenced content:

Mysql > SELECT*FROMreviews

+-+-

| | id | data |

+-+-

| | 1 | Gingerboyhasanewsingleoutcalled... |

| | 2 | Helloall,IreallylikethenewMadon... |

| | 3 | HaveyouheardthenewbandHotterThan... |

3rowsinset (0.00sec)

2. Define full-text search fields

Next, define the fields you want to use as a full-text search index

The following is the referenced content:

Mysql > ALTERTABLEreviewsADDFULLTEXTINDEX (data)

QueryOK,3rowsaffected (0.21sec)

Records:3Duplicates:0Warnings:0

Use the SHOWINDEXES command to check that the index has been added:

The following is the referenced content:

Mysql > SHOWINDEXESFROMreviews

+-+ +

| | Table | Column_name | Packed | Null | Index_type | Comment | |

-+

| | reviews | id | NULL | | BTREE |

Reviews | data | NULL | YES | FULLTEXT | |

+-+ +

The above is all the contents of the article "how to get better search results in MySQL". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Content search search results results commands articles tables full text fields indexes learning help next albums examples data text easy to understand more organized Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia Xiaomi MySQL macOS