Author: alien

  • Khóa học miễn phí MySQL – Arithmetic Operators nhận dự án làm có lương

    MySQL – Arithmetic Operators



    MySQL – Arithmetic Operators

    Arithmetic operators in MySQL are tools for performing mathematical calculations, similar to how we use them in basic math. They allow us to manipulate numeric values in your database, just like we would in simple math problems. Following are the arithmetic operators in MySQL −

    Sr.No. Name & Description
    1

    This operator divides left-hand operand by right-hand operand and, returns the remainder.

    2

    This operator multiplies values on either side of the operator.

    3

    This operator adds values on either side of the operator.

    4

    This operator subtracts right-hand operand from left-hand operand.

    5

    This operator is used to change the sign of the operand.

    6

    This operator divides left-hand operand by right-hand operand.

    7

    This operator performs the division operation and discards all the digits after the decimal.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MySQL – ngram Fulltext Parser nhận dự án làm có lương

    MySQL – ngram Full-Text Parser

    Table of content


    Usually in Full-Text searching, the built-in MySQL Full-Text parser considers the white spaces between words as delimiters. This determines where the words actually begin and end, to make the search simpler. However, this is only simple for languages that use spaces to separate words.

    Several ideographic languages like Chinese, Japanese and Korean languages do not use word delimiters. To support full-text searches in languages like these, an ngram parser is used. This parser is supported by both InnoDB and MyISAM storage engines.

    The ngram Full-Text Parser

    An ngram is a continuous sequence of ”n” characters from a given sequence of text. The ngram parser divides a sequence of text into tokens as a contiguous sequence of n characters.

    For example, consider the text ”Tutorial” and observe how it is tokenized by the ngram parser −

    n=1: ''T'', ''u'', ''t'', ''o'', ''r'', ''i'', ''a'', ''l''
    n=2: ''Tu'', ''ut'', ''to'' ''or'', ''ri'', ''ia'' ''al''
    n=3: ''Tut'', ''uto'', ''tor'', ''ori'', ''ria'', ''ial''
    n=4: ''Tuto'', ''utor'', ''tori'', ''oria'', ''rial''
    n=5: ''Tutor'', ''utori'', ''toria'', ''orial''
    n=6: ''Tutori'', ''utoria'', ''torial''
    n=7: ''Tutoria'', ''utorial''
    n=8: ''Tutorial''
    

    The ngram full-text parser is a built-in server plugin. As with other built-in server plug-ins, it is automatically loaded when the server is started.

    Configuring ngram Token Size

    To change the token size, from its default size 2, use the ngram_token_size configuration option. The range of ngram values is from 1 to 10. But to increase the speed of search queries, use smallers token sizes; as smaller token sizes allow faster searches with smaller full-text search indexes.

    Because ngram_token_size is a read-only variable, you can only set its value using two options:

    Setting the –ngram_token_size in startup string:

    mysqld --ngram_token_size=1
    

    Setting ngram_token_size in configuration file ”my.cnf”:

    [mysqld]
    
    ngram_token_size=1
    

    Creating FULLTEXT Index Using ngram Parser

    A FULLTEXT index can be created on columns of a table using the FULLTEXT keyword. This is used with CREATE TABLE, ALTER TABLE or CREATE INDEX SQL statements; you just have to specify ”WITH PARSER ngram”. Following is the syntax −

    CREATE TABLE table_name (
       column_name1 datatype,
       column_name2 datatype,
       column_name3 datatype,
       ...
       FULLTEXT (column_name(s)) WITH PARSER NGRAM
    ) ENGINE=INNODB CHARACTER SET UTF8mb4;
    

    Example

    In this example, we are creating a FULLTEXT index using the CREATE TABLE statement as follows −

    CREATE TABLE blog (
       ID INT AUTO_INCREMENT NOT NULL,
       TITLE VARCHAR(255),
       DESCRIPTION TEXT,
       FULLTEXT ( TITLE, DESCRIPTION ) WITH PARSER NGRAM,
       PRIMARY KEY(id)
    ) ENGINE=INNODB CHARACTER SET UTF8MB4;
    
    SET NAMES UTF8MB4;
    

    Now, insert data (in any ideographic language) into this table created −

    INSERT INTO BLOG VALUES
    (NULL, ''教程'', ''教程是对一个概念的冗长研究''),
    (NULL, ''文章'', ''文章是关于一个概念的基于事实的小信息'');
    

    To check how the text is tokenized, execute the following statements −

    SET GLOBAL innodb_ft_aux_table = "customers/blog";
    
    SELECT * FROM
    INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE
    ORDER BY doc_id, position;
    

    ngram Parser Space Handling

    Any whitespace character is eliminated in the ngram parser when parsing. For instance, consider the following TEXT with token size 2 −

    • “ab cd” is parsed to “ab”, “cd”

    • “a bc” is parsed to “bc”

    ngram Parser Stop word Handling

    Apart from the whitespace character, MySQL has a stop word list consisting of various that are considered to be stopwords. If the parser encounters any word in the text present in the stopword list, the word is excluded from the index.

    Normal Phrase searches are converted to ngram phrase searches. For example, The search phrase “abc” is converted to “ab bc”, which returns documents containing “abc” and “ab bc”; and the search phrase “abc def” is converted to “ab bc de ef”, which returns documents containing “abc def” and “ab bc de ef”. A document that contains “abcdef” is not returned.

    For natural language mode search, the search term is converted to a union of ngram terms. For example, the string “abc” (assuming ngram_token_size=2) is converted to “ab bc”. Given two documents, one containing “ab” and the other containing “abc”, the search term “ab bc” matches both documents.

    For boolean mode search, the search term is converted to an ngram phrase search. For example, the string ”abc” (assuming ngram_token_size=2) is converted to ””ab bc””. Given two documents, one containing ”ab” and the other containing ”abc”, the search phrase ””ab bc”” only matches the document containing ”abc”.

    Because an ngram FULLTEXT index contains only ngrams, and does not contain information about the beginning of terms, wildcard searches may return unexpected results. The following behaviors apply to wildcard searches using ngram FULLTEXT search indexes:

    • If the prefix term of a wildcard search is shorter than ngram token size, the query returns all indexed rows that contain ngram tokens starting with the prefix term. For example, assuming ngram_token_size=2, a search on “a*” returns all rows starting with “a”.

    • If the prefix term of a wildcard search is longer than ngram token size, the prefix term is converted to an ngram phrase and the wildcard operator is ignored. For example, assuming ngram_token_size=2, an “abc*” wildcard search is converted to “ab bc”.

    ngram Full-Text Parser Using a Client Program

    We can also perform ngram full-text parser operation using the client program.

    Syntax

    To perform the ngram fulltext parser through a PHP programe, we need to execute the “Create” statement using the mysqli function query() as follows −

    $sql = "CREATE TABLE blog (ID INT AUTO_INCREMENT NOT NULL, title VARCHAR(255), DESCRIPTION TEXT, FULLTEXT ( title, DESCRIPTION ) WITH PARSER NGRAM, PRIMARY KEY(id) )ENGINE=INNODB CHARACTER SET UTF8MB4";
    $mysqli->query($sql);
    

    To perform the ngram fulltext parser through a JavaScript program, we need to execute the “Create” statement using the query() function of mysql2 library as follows −

    sql = `CREATE TABLE blog (ID INT AUTO_INCREMENT NOT NULL, title VARCHAR(255), DESCRIPTION TEXT, FULLTEXT ( title, DESCRIPTION ) WITH PARSER NGRAM, PRIMARY KEY(id) )ENGINE=INNODB CHARACTER SET UTF8MB4`;
    con.query(sql);
    

    To perform the ngram fulltext parser through a Java program, we need to execute the “Create” statement using the JDBC function execute() as follows −

    String sql = "CREATE TABLE blog (ID INT AUTO_INCREMENT NOT NULL, title VARCHAR(255), description TEXT," +
    " FULLTEXT ( title, description ) WITH PARSER NGRAM, PRIMARY KEY(id) )ENGINE=INNODB CHARACTER SET UTF8MB4";
    statement.execute(sql);
    

    To perform the ngram fulltext parser through a python program, we need to execute the “Create” statement using the execute() function of the MySQL Connector/Python as follows −

    create_table_query = ''CREATE TABLE blog(ID INT AUTO_INCREMENT NOT NULL, title VARCHAR(255), description TEXT, FULLTEXT (title, description) WITH PARSER NGRAM, PRIMARY KEY(id)) ENGINE=INNODB CHARACTER SET UTF8MB4''
    cursorObj.execute(queryexpansionfulltext_search)
    

    Example

    Following are the programs −

    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "password";
    $dbname = "TUTORIALS";
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    	
    ", $mysqli->connect_error); exit(); } // printf(''Connected successfully.
    ''); /*CREATE Table*/ $sql = "CREATE TABLE blog (ID INT AUTO_INCREMENT NOT NULL, title VARCHAR(255), description TEXT, FULLTEXT ( title, description ) WITH PARSER NGRAM, PRIMARY KEY(id) )ENGINE=INNODB CHARACTER SET UTF8MB4"; $result = $mysqli->query($sql); if ($result) { printf("Table created successfully...!n"); } //insert data $q = "INSERT INTO blog (id, title, description) VALUES (NULL, ''教程'', ''教程是对一个概念的冗长研究''), (NULL, ''文章'', ''文章是关于一个概念的基于事实的小信息'')"; if ($res = $mysqli->query($q)) { printf("Data inserted successfully...!n"); } //we will use the below statement to see how the ngram tokenizes the data: $setglobal = "SET GLOBAL innodb_ft_aux_table = ''TUTORIALS/blog''"; if ($mysqli->query($setglobal)) { echo "global innodb_ft_aux_table set...!"; } $s = "SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE ORDER BY doc_id, position "; if ($r = $mysqli->query($s)) { print_r($r); } //display data (ngram parser phrase search); $query = "SELECT * FROM blog WHERE MATCH (title, description) AGAINST (''教程'')"; if ($r = $mysqli->query($query)) { printf("Table Records: n"); while ($row = $r->fetch_assoc()) { printf( "ID: %d, Title: %s, Descriptions: %s", $row["id"], $row["title"], $row["description"] ); printf("n"); } } else { printf("Failed"); } $mysqli->close();

    Output

    The output obtained is as shown below −

    global innodb_ft_aux_table set...!mysqli_result Object
    (
        [current_field] => 0
        [field_count] => 6
        [lengths] =>
        [num_rows] => 62
        [type] => 0
    )
    Table Records:
    ID: 1, Title: 教程, Descriptions: 教程是对一个概念的冗长研究
    ID: 3, Title: 教程, Descriptions: 教程是对一个概念的冗长研究
    
    var mysql = require("mysql2");
    var con = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "password",
    }); //Connecting to MySQL
    
    con.connect(function (err) {
      if (err) throw err;
      //   console.log("Connected successfully...!");
      //   console.log("--------------------------");
      sql = "USE TUTORIALS";
      con.query(sql);
    
      //create a table...
      sql = `CREATE TABLE blog (ID INT AUTO_INCREMENT NOT NULL, title VARCHAR(255), description TEXT, FULLTEXT ( title, description ) WITH PARSER NGRAM, PRIMARY KEY(id) )ENGINE=INNODB CHARACTER SET UTF8MB4`;
      con.query(sql);
    
      //insert data
      sql = `INSERT INTO blog (id, title, description) VALUES
      (NULL, ''教程'', ''教程是对一个概念的冗长研究''),
      (NULL, ''文章'', ''文章是关于一个概念的基于事实的小信息'')`;
      con.query(sql);
    
      //we will use the below statement to see how the ngram tokenizes the data:
      sql = "SET GLOBAL innodb_ft_aux_table = ''TUTORIALS/blog''";
      con.query(sql);
    
      //display the table details;
      sql = `SELECT * FROM blog WHERE MATCH (title, description) AGAINST (''教程'')`;
      con.query(sql, function (err, result) {
        if (err) throw err;
        console.log(result);
      });
    });
    

    Output

    The output obtained is as shown below −

    [ { id: 1, title: ''教程'', description: ''教程是对一个概念的冗长研究'' } ]
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class NgRamFSearch {
       public static void main(String[] args) {
          String url = "jdbc:mysql://localhost:3306/TUTORIALS";
          String username = "root";
          String password = "password";
          try {
             Class.forName("com.mysql.cj.jdbc.Driver");
             Connection connection = DriverManager.getConnection(url, username, password);
             Statement statement = connection.createStatement();
             System.out.println("Connected successfully...!");
    
             //creating a table that takes fulltext column with parser ngram...!
             String sql = "CREATE TABLE blog (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255), description TEXT," +
             " FULLTEXT ( title, description ) WITH PARSER NGRAM, PRIMARY KEY(id) )ENGINE=INNODB CHARACTER SET UTF8MB4";
             statement.execute(sql);
             //System.out.println("Table created successfully...!");
    
             //inserting data to the table
             String insert = "INSERT INTO blog (id, title, description) VALUES (NULL, ''教程'', ''教程是对一个概念的冗长研究'')," +
                     " (NULL, ''文章'', ''文章是关于一个概念的基于事实的小信息'')";
             statement.execute(insert);
             //System.out.println("Data inserted successfully...!");
    
             //we will use the below statement to see how the ngram tokenizes the data:
             String set_global = "SET GLOBAL innodb_ft_aux_table = ''TUTORIALS/blog''";
             statement.execute(set_global);
    
             ResultSet resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE ORDER BY doc_id, position ");
             System.out.println("Information schema order by Id...!");
             while (resultSet.next()){
                System.out.println(resultSet.getString(1)+" "+resultSet.getString(2));
             }
    
             //displaying the data...!
             String query = "SELECT * FROM blog WHERE MATCH (title, description) AGAINST (''教程'')";
             ResultSet resultSet1 =  statement.executeQuery(query);
             System.out.println("table records:");
             while (resultSet1.next()){
                System.out.println(resultSet1.getString(1)+" "+resultSet1.getString(2)+ " "+resultSet1.getString(3));
             }
    
             connection.close();
          } catch (Exception e) {
             System.out.println(e);
          }
       }
    }
    

    Output

    The output obtained is as shown below −

    Connected successfully...!
    Information schema order by Id...!
    教程 2
    教程 2
    程是 2
    是对 2
    对一 2
    一个 2
    个概 2
    概念 2
    念的 2
    的冗 2
    冗长 2
    长研 2
    研究 2
    文章 3
    文章 3
    章是 3
    是关 3
    关于 3
    于一 3
    一个 2
    个概 2
    概念 2
    念的 2
    的基 3
    基于 3
    于事 3
    事实 3
    实的 3
    的小 3
    小信 3
    信息 3
    table records:
    1 教程 教程是对一个概念的冗长研究
    
    import mysql.connector
    # Establishing the connection
    connection = mysql.connector.connect(
        host=''localhost'',
        user=''root'',
        password=''password'',
        database=''tut''
    )
    # Creating a cursor object
    cursorObj = connection.cursor()
    # Create the blog table with NGRAM full-text parser
    create_table_query = ''''''
    CREATE TABLE blog (
        id INT AUTO_INCREMENT NOT NULL,
        title VARCHAR(255),
        description TEXT,
        FULLTEXT (title, description) WITH PARSER NGRAM,
        PRIMARY KEY(id)
    ) ENGINE=INNODB CHARACTER SET UTF8MB4;
    ''''''
    cursorObj.execute(create_table_query)
    print("Table ''blog'' is created successfully!")
    # Set the character set to UTF8MB4
    set_charset_query = "SET NAMES UTF8MB4;"
    cursorObj.execute(set_charset_query)
    print("Character set is set to UTF8MB4.")
    # Insert data into the blog table
    data_to_insert = [
        (''教程'', ''教程是对一个概念的冗长研究''),
        (''文章'', ''文章是关于一个概念的基于事实的小信息'')
    ]
    insert_query = "INSERT INTO blog (title, description) VALUES (%s, %s)"
    cursorObj.executemany(insert_query, data_to_insert)
    connection.commit()
    print("Data inserted into the ''blog'' table.")
    # Query the INNODB_FT_INDEX_CACHE table to get the full-text index information
    query_index_cache_query = "SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE ORDER BY doc_id, position;"
    cursorObj.execute(query_index_cache_query)
    results = cursorObj.fetchall()
    print("Results of INNODB_FT_INDEX_CACHE table:")
    for row in results:
        print(row)
    # Close the cursor and connection
    cursorObj.close()
    connection.close()
    

    Output

    The output obtained is as shown below −

    Table ''blog'' is created successfully!
    Character set is set to UTF8MB4.
    Data inserted into the ''blog'' table.
    Results of INNODB_FT_INDEX_CACHE table:
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MySQL – Query Expansion Fulltext Search nhận dự án làm có lương

    MySQL – Query Expansion Full-Text Search

    Table of content


    In relational databases like MySQL, Full-text search is a technique used to retrieve result-sets that might not perfectly match the search keyword. This type of search is useful in cases where the keywords used for searching do not match the results a user expects. So, this searching technique is designed to focus on increasing search relevance in order to reduce the accuracy gap between search queries and search results. Thus, search results are displayed in the order of highest to the lowest relevancy to the search keyword.

    There are three types of search modes used with Full-text search −

    • Natural Language Mode

    • Query Expansion Mode

    • Boolean Mode

    Search is always done by the user with the limited knowledge they possess. Thus, there are cases when the search keywords are way too short to conduct a proper search. This is where Blind Expansion Search technique comes into picture.

    Blind Expansion Search, also known as Automatic Relevance Feedback, is used to widen the search results based on additional keywords that are closely related to the original keywords. It is enabled using the ”WITH QUERY EXPANSION” search phrase.

    The search is performed twice in this cases by following the steps given below −

    Step 1 − All the rows that match the given search keyword are searched first.

    Step 2 − These obtained rows are then checked for relevant words to the original keyword in them.

    Step 3 − Finally, the rows are searched again based on these relevant words instead of the original keywords specified by the users.

    To perform the query expansion full-text search on a database table, the WITH QUERY EXPANSION or IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION search modifiers must be specified in the AGAINST() function.

    Example

    Let us understand how to perform Query Expansion Full-text Search on a database table in the following example.

    For that, we will first create a table named DBMS_TUTORIALS containing the title and description of an article. The FULLTEXT index is applied on text columns TUTORIAL_TITLE and DESCRIPTIONS as shown below −

    CREATE TABLE DBMS_TUTORIALS(
       TUTORIAL_TITLE VARCHAR(200),
       DESCRIPTIONS TEXT,
       FULLTEXT(TUTORIAL_TITLE, DESCRIPTIONS)
    );
    

    Now, let us insert details about tutorials, like their titles and descriptions, into this table using the following queries −

    INSERT INTO DBMS_TUTORIALS VALUES
    (''MySQL Tutorial'', ''MySQL is an RDBMS that uses SQL to structure the data stored''),
    (''ORACLE Tutorial'', ''ORACLE is an RDBMS that uses SQL to structure the data stored''),
    (''MySQL Security'', ''MySQL Database can store sensitive data, so security is required''),
    (''MySQL vs MariaDB'', ''Comparing two databases...''),
    (''JDBC Tutorial'', ''In this Java-based database connectivity...'');
    

    The table is created as −

    TUTORIAL_TITLE DESCRIPTIONS
    MySQL Tutorial MySQL is an RDBMS that uses SQL to structure the data stored
    ORACLE Tutorial ORACLE is an RDBMS that uses SQL to structure the data stored
    MySQL Security MySQL Database can store sensitive data, so security is required
    MySQL vs MariaDB Comparing two databases…
    JDBC Tutorial In this Java-based database connectivity…

    Using the Query Expansion Mode in full-text search, we search for records of articles relevant to data, with the keyword ‘RDBMS’

    SELECT * FROM DBMS_TUTORIALS
    WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS)
    AGAINST (''RDBMS'' WITH QUERY EXPANSION);
    

    Output

    The output is obtained as −

    TUTORIAL_TITLE DESCRIPTIONS
    ORACLE Tutorial ORACLE is an RDBMS that uses SQL to structure the data stored
    MySQL Tutorial MySQL is an RDBMS that uses SQL to structure the data stored
    MySQL Security MySQL Database can store sensitive data, so security is required
    MySQL vs MariaDB Comparing two databases…
    JDBC Tutorial In this Java-based database connectivity…

    IN NATURAL LANGUAGE MODE

    In the result-set obtained above, all tutorial records are about databases, which is why the query retrieved all the records ordered based on relevance.

    SELECT * FROM DBMS_TUTORIALS
    WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS)
    AGAINST (''Security'' IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION);
    

    Output

    The output is obtained as −

    TUTORIAL_TITLE DESCRIPTIONS
    MySQL Security MySQL Database can store sensitive data, so security is required
    JDBC Tutorial In this Java-based database connectivity…
    MySQL Tutorial MySQL is an RDBMS that uses SQL to structure the data stored
    ORACLE Tutorial ORACLE is an RDBMS that uses SQL to structure the data stored
    MySQL vs MariaDB Comparing two databases…

    In this result-set, even if the search keyword is ”Security”, the actual security related tutorials are just ”MySQL Security” and ”JDBC Tutorial”, so they are retrieved first. These records are then followed by database related records as an expanded query.

    Query Expansion Full-Text Search Using Client Program

    We can also Perform Query expansion full-text search operation using the client program.

    Syntax

    To perform the Query Expansion Full-Text Search through a PHP program, we need to execute the SELECT statement using the mysqli function query() as follows −

    $sql = "SELECT * FROM DBMS_TUTORIALS WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS) AGAINST (''RDBMS'' WITH QUERY EXPANSION)";
    $mysqli->query($sql);
    

    To perform the Query Expansion Full-Text search through a JavaScript program, we need to execute the SELECT statement using the query() function of mysql2 library as follows −

    sql = "SELECT * FROM DBMS_TUTORIALS WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS) AGAINST (''RDBMS'' WITH QUERY EXPANSION)";
    con.query(sql);
    

    To perform the Query Expansion Full-Text Search through a Java program, we need to execute the SELECT statement using the JDBC function executeQuery() as follows −

    String sql = "SELECT * FROM DBMS_TUTORIALS WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS) AGAINST (''RDBMS'' WITH QUERY EXPANSION)";
    statement.executeQuery(sql);
    

    To perform the Query Expansion Full-Text Search through a Python program, we need to execute the SELECT statement using the execute() function of the MySQL Connector/Python as follows −

    queryexpansionfulltext_search = ''SELECT * FROM DBMS_TUTORIALS WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS)  AGAINST (''RDBMS'' WITH QUERY EXPANSION)''
    cursorObj.execute(queryexpansionfulltext_search)
    

    Example

    Following are the programs −

    $dbhost = ''localhost
    $dbuser = ''root
    $dbpass = ''password
    $dbname = ''TUTORIALS
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error); exit(); } // printf(''Connected successfully.
    ''); /*CREATE Table*/ $sql = "CREATE TABLE DBMS_TUTORIALS(TUTORIAL_TITLE VARCHAR(200), DESCRIPTIONS TEXT, FULLTEXT(TUTORIAL_TITLE, DESCRIPTIONS))"; $result = $mysqli->query($sql); if ($result) { printf("Table created successfully...!n"); } //insert data $q = "INSERT INTO DBMS_TUTORIALS (TUTORIAL_TITLE , DESCRIPTIONS) VALUES (''MySQL Tutorial'', ''MySQL is an RDBMS that uses SQL to structure the data stored''), (''ORACLE Tutorial'', ''ORACLE is an RDBMS that uses SQL to structure the data stored''), (''MySQL Security'', ''MySQL Database can store sensitive data, so security is required''), (''MySQL vs MariaDB'', ''Comparing two databases...''), (''JDBC Tutorial'', ''In this Java-based database connectivity...'')"; if ($res = $mysqli->query($q)) { printf("Data inserted successfully...!n"); } //Using the Query Expansion Mode in Full-text search, try to search for records of DBMS_TUTORIALS relevant to data, with the keyword ''RDBMS'' $s = "SELECT * FROM DBMS_TUTORIALS WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS) AGAINST (''RDBMS'' WITH QUERY EXPANSION)"; if ($r = $mysqli->query($s)) { printf("Table Records: n"); while ($row = $r->fetch_assoc()) { printf("Tutorial_title: %s, Descriptions: %s", $row["TUTORIAL_TITLE"], $row["DESCRIPTIONS"]); printf("n"); } } else { printf(''Failed''); } $mysqli->close();

    Output

    The output obtained is as shown below −

    Table created successfully...!
    Data inserted successfully...!
    Table Records:
    Tutorial_title: ORACLE Tutorial, Descriptions: ORACLE is an RDBMS that uses SQL to structure the data stored
    Tutorial_title: MySQL Tutorial, Descriptions: MySQL is an RDBMS that uses SQL to structure the data stored
    Tutorial_title: MySQL Security, Descriptions: MySQL Database can store sensitive data, so security is required
    Tutorial_title: MySQL vs MariaDB, Descriptions: Comparing two databases...
    Tutorial_title: JDBC Tutorial, Descriptions: In this Java-based database connectivity...
    
    var mysql = require("mysql2");
    var con = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "password",
    }); //Connecting to MySQL
    
    con.connect(function (err) {
      if (err) throw err;
      //   console.log("Connected successfully...!");
      //   console.log("--------------------------");
      sql = "USE TUTORIALS";
      con.query(sql);
    
      //create a table...
      sql =
        "CREATE TABLE DBMS_TUTORIALS(TUTORIAL_TITLE VARCHAR(200), DESCRIPTIONS TEXT, FULLTEXT(TUTORIAL_TITLE, DESCRIPTIONS))";
      con.query(sql);
    
      //insert data
      sql = `INSERT INTO DBMS_TUTORIALS (TUTORIAL_TITLE , DESCRIPTIONS) VALUES
      (''MySQL Tutorial'', ''MySQL is an RDBMS that uses SQL to structure the data stored''),
      (''ORACLE Tutorial'', ''ORACLE is an RDBMS that uses SQL to structure the data stored''),
      (''MySQL Security'', ''MySQL Database can store sensitive data, so security is required''),
      (''MySQL vs MariaDB'', ''Comparing two databases...''),
      (''JDBC Tutorial'', ''In this Java-based database connectivity...'')`;
    
      con.query(sql);
    
      //Using the Query Expansion Mode in Full-text search, try to search for records of DBMS_TUTORIALS relevant to data, with the keyword ''RDBMS''
      sql = "SELECT * FROM DBMS_TUTORIALS WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS) AGAINST (''RDBMS'' WITH QUERY EXPANSION)";
      con.query(sql, function (err, result) {
        if (err) throw err;
        console.log(result);
      });
    });
    

    Output

    The output obtained is as shown below −

    [
        {
          TUTORIAL_TITLE: ''ORACLE Tutorial'',
          DESCRIPTIONS: ''ORACLE is an RDBMS that uses SQL to structure the data stored''
        },
        {
          TUTORIAL_TITLE: ''MySQL Tutorial'',
          DESCRIPTIONS: ''MySQL is an RDBMS that uses SQL to structure the data stored''
        },
        {
          TUTORIAL_TITLE: ''MySQL Security'',
          DESCRIPTIONS: ''MySQL Database can store sensitive data, so security is required''
        },
        {
          TUTORIAL_TITLE: ''MySQL vs MariaDB'',
          DESCRIPTIONS: ''Comparing two databases...''
        },
        {
          TUTORIAL_TITLE: ''JDBC Tutorial'',
          DESCRIPTIONS: ''In this Java-based database connectivity...''
        }
    ]
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class QeFSearch {
       public static void main(String[] args) {
          String url = "jdbc:mysql://localhost:3306/TUTORIALS";
          String username = "root";
          String password = "password";
          try {
             Class.forName("com.mysql.cj.jdbc.Driver");
             Connection connection = DriverManager.getConnection(url, username, password);
             Statement statement = connection.createStatement();
             System.out.println("Connected successfully...!");
    
             //creating a table that takes fulltext column...!
             String sql = "CREATE TABLE DBMS_TUTORIALS(TUTORIAL_TITLE VARCHAR(200), DESCRIPTIONS TEXT, FULLTEXT(TUTORIAL_TITLE, DESCRIPTIONS))";
             statement.execute(sql);
             System.out.println("Table created successfully...!");
    
             //inserting data to the table
             String insert = "INSERT INTO DBMS_TUTORIALS (TUTORIAL_TITLE , DESCRIPTIONS) VALUES" +
             "(''MySQL Tutorial'', ''MySQL is an RDBMS that uses SQL to structure the data stored'')," +
             "(''ORACLE Tutorial'', ''ORACLE is an RDBMS that uses SQL to structure the data stored'')," +
             "(''MySQL Security'', ''MySQL Database can store sensitive data, so security is required'')," +
             "(''MySQL vs MariaDB'', ''Comparing two databases...'')," +
             "(''JDBC Tutorial'', ''In this Java-based database connectivity...'')";
             statement.execute(insert);
             System.out.println("Data inserted successfully...!");
    
             //Using the Query Expansion Mode in Full-text search, try to search for records of DBMS_TUTORIALS relevant to data, with the keyword ''RDBMS''...!
             ResultSet resultSet = statement.executeQuery("SELECT * FROM DBMS_TUTORIALS WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS) AGAINST (''RDBMS'' WITH QUERY EXPANSION)");
             while (resultSet.next()){
                System.out.println(resultSet.getString(1)+" "+resultSet.getString(2));
             }
             connection.close();
          } catch (Exception e) {
             System.out.println(e);
          }
       }
    }
    

    Output

    The output obtained is as shown below −

    Connected successfully...!
    Table created successfully...!
    Data inserted successfully...!
    ORACLE Tutorial ORACLE is an RDBMS that uses SQL to structure the data stored
    MySQL Tutorial MySQL is an RDBMS that uses SQL to structure the data stored
    MySQL Security MySQL Database can store sensitive data, so security is required
    MySQL vs MariaDB Comparing two databases...
    JDBC Tutorial in this Java-based database connectivity...
    
    import mysql.connector
    # Establishing the connection
    connection = mysql.connector.connect(
        host=''localhost'',
        user=''root'',
        password=''password'',
        database=''tut''
    )
    # Creating a cursor object
    cursorObj = connection.cursor()
    queryexpansionfulltext_search = ''''''
    SELECT * FROM DBMS_TUTORIALS
    WHERE MATCH(TUTORIAL_TITLE, DESCRIPTIONS)
    AGAINST (''RDBMS'' WITH QUERY EXPANSION)
    ''''''
    cursorObj.execute(queryexpansionfulltext_search)
    # Fetching all the results
    results = cursorObj.fetchall()
    # Display the result
    print("Query expansion Fulltext search results:")
    for row in results:
        print(row)
    cursorObj.close()
    connection.close()
    

    Output

    The output obtained is as shown below −

    Query expansion Fulltext search results:
    (''ORACLE Tutorial'', ''ORACLE is an RDBMS that uses SQL to structure the data stored'')
    (''MySQL Tutorial'', ''MySQL is an RDBMS that uses SQL to structure the data stored'')
    (''MySQL Security'', ''MySQL Database can store sensitive data, so security is required'')
    (''MySQL vs MariaDB'', ''Comparing two databases...'')
    (''JDBC Tutorial'', ''In this Java-based database connectivity...'')
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MySQL – Natural Language Fulltext Search nhận dự án làm có lương

    MySQL – Natural Language Fulltext Search

    Table of content


    Before we fully get into the concept of Natural Language Full-text Search, let us try to understand the context of it. Nowadays, the keywords used for searches might not always match the results that users expect. So search engines are designed to focus on increasing search relevance to reduce the accuracy gap between search queries and search results. Thus, results are displayed in order of most relevance to the search keyword.

    Similarly, in relational databases like MySQL, full-text search is a technique used to retrieve result-sets that might not perfectly match the search keyword. There are three types of search modes used with full-text search −

    • Natural Language Mode

    • Query Expansion Mode

    • Boolean Mode

    The Natural Language Full-text search performs the usual Full-text search in the IN NATURAL LANGUAGE mode. When a Full-text search is performed in this mode, the search results are displayed in the order of their relevance to the keyword (against which this search is performed). This is the default mode for the Full-text search.

    Since this is a Full-text search, the FULLTEXT indexes must be applied on text-based columns (like CHAR, VARCHAR, TEXT datatype columns). The FULLTEXT index is a special type of index that is used to search for the keywords in the text values instead of trying to compare the keyword with these column values.

    Syntax

    Following is the basic syntax to perform the Natural Language Full-text Search −

    SELECT * FROM table_name
    WHERE MATCH(column_name(s))
    AGAINST (''keyword_name'' IN NATURAL LANGUAGE MODE);
    

    Example

    Let us understand how to perform Natural Language Full-text Search on a database table in the following example.

    For that, we will first create a table named ARTICLES containing the title and description of an article. The FULLTEXT index is applied on text columns article_title and descriptions as shown below −

    CREATE TABLE ARTICLES (
       ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
       ARTICLE_TITLE VARCHAR(100),
       DESCRIPTION TEXT,
       FULLTEXT (ARTICLE_TITLE, DESCRIPTION)
    ) ENGINE = InnoDB;
    

    Now, let us insert details about articles, like their titles and DESCRIPTION, into this table using the following queries −

    INSERT INTO ARTICLES (ARTICLE_TITLE, DESCRIPTION) VALUES
    (''MySQL Tutorial'', ''MySQL is a relational database system that uses SQL to structure data stored''),
    (''Java Tutorial'', ''Java is an object-oriented and platform-independent programming language''),
    (''Hadoop Tutorial'', ''Hadoop is framework that is used to process large sets of data''),
    (''Big Data Tutorial'', ''Big Data refers to data that has wider variety of data sets in larger numbers''),
    (''JDBC Tutorial'', ''JDBC is a Java based technology used for database connectivity'');
    

    The table is created is as follows −

    ID ARTICLE_TITLE DESCRIPTION
    1 MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored
    2 Java Tutorial Java is an object-oriented and platform-independent programming language
    3 Hadoop Tutorial Hadoop is framework that is used to process large sets of data
    4 Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    5 JDBC Tutorial JDBC is a Java based technology used for database connectivity

    Using the Natural Language Mode in Full-text search, search for records of articles relevant to data, with the keyword ”data set”.

    SELECT * FROM ARTICLES
    WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)
    AGAINST (''data set'' IN NATURAL LANGUAGE MODE);
    

    Output

    Following is the output −

    ID ARTICLE_TITLE DESCRIPTION
    4 Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    1 MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored
    3 Hadoop Tutorial Hadoop is framework that is used to process large sets of data

    As we see above, among all the articles present in the table, three search results are obtained which are relevant to the term ”data set” and are arranged in the order of their relevance. But note how keyword ”data set” is not a perfect match in the ”MySQL Tutorial” article record and its still retrieved because MySQL deals with data sets as well.

    The Natural Language Full-text Search uses tf-idf algorithm, where ”tf” refers to term frequency and ”idf” is inverse document frequency. The search refers to the frequency of a word in a single document, and the number of documents the word is present in. However, there are some words that the search usually ignores, like words having less than certain characters. InnoDB ignores words with less than 3 characters while MyISAM ignores words less than 4 characters. Such words are known as Stopwords (the, a, an, are etc.).

    Example

    In the following example, we are performing a simple Natural Language Full-text Search on the ARTICLES Table created above. Let us see how stop words impact the Full-text search by performing it against two keywords: ”Big Tutorial” and ”is Tutorial”.

    Searching ”Big Tutorial”:

    Following query performs the full-text search in Natural Language Mode against ”Big Tutorial” keyword −

    SELECT ARTICLE_TITLE, DESCRIPTION FROM ARTICLES
    WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)
    AGAINST (''Big Tutorial'' IN NATURAL LANGUAGE MODE);
    

    Output:

    The output is obtained as −

    ARTICLE_TITLE DESCRIPTION
    Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored
    Java Tutorial Java is an object-oriented and platform-independent programming language
    Hadoop Tutorial Hadoop is framework that is used to process large sets of data
    JDBC Tutorial JDBC is a Java based technology used for database connectivity

    Searching ”is Tutorial”:

    Following query performs the full-text search in Natural Language Mode against ”is Tutorial” keyword −

    SELECT ARTICLE_TITLE, DESCRIPTION FROM Articles
    WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)
    AGAINST (''is Tutorial'' IN NATURAL LANGUAGE MODE);
    

    Output:

    The output is obtained as −

    ARTICLE_TITLE DESCRIPTION
    MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored
    Java Tutorial Java is an object-oriented and platform-independent programming language
    Hadoop Tutorial Hadoop is framework that is used to process large sets of data
    Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    JDBC Tutorial JDBC is a Java based technology used for database connectivity

    As we see in the example above, since the word ”Tutorial” is present in all the records of the table, all of them are retrieved in both cases. However, the order of relevance is determined by the second word of the keyword specified.

    In the first case, as the word ”Big” is present in ”Big Data Tutorial”, that record is retrieved first. In the second case, the order of records in the result-set are the same as that of original table since the word ”is” is a stop word, so it is ignored.

    Natural-language-Fulltext-search Using a Client Program

    We can also Perform Natural-language-fulltext-search operation on a MySQL database using the client program.

    Syntax

    To perform the Natural-language-Fulltext-search through a PHP program, we need to execute the following SELECT statement using the mysqli function query() as follows −

    $sql = "SELECT * FROM Articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)  AGAINST (''data set'' IN NATURAL LANGUAGE MODE)";
    $mysqli->query($sql);
    

    To perform the Natural-language-Fulltext-search through a JavaScript program, we need to execute the following SELECT statement using the query() function of mysql2 library as follows −

    sql = `SELECT * FROM Articles  WHERE MATCH(ARTICLE_TITLE, DESCRIPTION) AGAINST (''data set'' IN NATURAL LANGUAGE MODE)`;
    con.query(sql);
    

    To perform the Natural-language-Fulltext-search through a Java program, we need to execute the SELECT statement using the JDBC function executeQuery() as follows −

    String sql = "SELECT * FROM Articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)  AGAINST (''data set'' IN NATURAL LANGUAGE MODE)";
    statement.executeQuery(sql);
    

    To perform the Natural-language-Fulltext-search through a python program, we need to execute the SELECT statement using the execute() function of the MySQL Connector/Python as follows −

    natural_language_search_query = ''SELECT * FROM Articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION) AGAINST (''data set'' IN NATURAL LANGUAGE MODE)''
    cursorObj.execute(natural_language_search_query)
    

    Example

    Following are the programs −

    $dbhost = ''localhost
    $dbuser = ''root
    $dbpass = ''password
    $dbname = ''TUTORIALS
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error); exit(); } // printf(''Connected successfully.
    ''); $s = "SELECT * FROM Articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION) AGAINST (''data set'' IN NATURAL LANGUAGE MODE)"; if ($r = $mysqli->query($s)) { printf("Table Records: n"); while ($row = $r->fetch_assoc()) { printf(" ID: %d, Title: %s, Descriptions: %s", $row["id"], $row["ARTICLE_TITLE"], $row["DESCRIPTION"]); printf("n"); } } else { printf(''Failed''); } $mysqli->close();

    Output

    The output obtained is as shown below −

    Table Records:
    ID: 4, Title: Big Data Tutorial, Descriptions: Big Data refers to data that has wider variety of data sets in larger numbers
    ID: 1, Title: MySQL Tutorial, Descriptions: MySQL is a relational database system that uses SQL to structure data stored
    ID: 3, Title: Hadoop Tutorial, Descriptions: Hadoop is framework that is used to process large sets of data
    
    var mysql = require("mysql2");
    var con = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "password",
    }); //Connecting to MySQL
    
    con.connect(function (err) {
      if (err) throw err;
      //   console.log("Connected successfully...!");
      //   console.log("--------------------------");
      sql = "USE TUTORIALS";
      con.query(sql);
    
      //display the table details!...
      sql = `SELECT * FROM Articles  WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)  AGAINST (''data set'' IN NATURAL LANGUAGE MODE)`;
      con.query(sql, function (err, result) {
        if (err) throw err;
        console.log(result);
      });
    });
    

    Output

    The output obtained is as shown below −

    We get the following output, after executing the above NodeJs Program.
    [
      {
        id: 4,
        ARTICLE_TITLE: ''Big Data Tutorial'',
        DESCRIPTION: ''Big Data refers to data that has wider variety of data sets in larger numbers''
      },
      {
        id: 1,
        ARTICLE_TITLE: ''MySQL Tutorial'',
        DESCRIPTION: ''MySQL is a relational database system that uses SQL to structure data stored''
      },
      {
        id: 3,
        ARTICLE_TITLE: ''Hadoop Tutorial'',
        DESCRIPTION: ''Hadoop is framework that is used to process large sets of data''
      }
    ]
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class NaturalLanguageSearch {
       public static void main(String[] args) {
          String url = "jdbc:mysql://localhost:3306/TUTORIALS";
          String username = "root";
          String password = "password";
          try {
             Class.forName("com.mysql.cj.jdbc.Driver");
             Connection connection = DriverManager.getConnection(url, username, password);
             Statement statement = connection.createStatement();
             System.out.println("Connected successfully...!");
    
             //displaying the fulltext records in the Natural language mode:
             ResultSet resultSet = statement.executeQuery("SELECT * FROM Articles WHERE MATCH(ARTICLE_TITLE, descriptions)  AGAINST (''data set'' IN NATURAL LANGUAGE MODE)");
             while (resultSet.next()){
                System.out.println(resultSet.getString(1)+" "+resultSet.getString(2)+ " "+resultSet.getString(3));
             }
             connection.close();
          } catch (Exception e) {
             System.out.println(e);
          }
       }
    }
    

    Output

    The output obtained is as shown below −

    Connected successfully...!
    4 Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    1 MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored
    3 Hadoop Tutorial Hadoop is framework that is used to process large sets of data
    
    import mysql.connector
    # Establishing the connection
    connection = mysql.connector.connect(
       host=''localhost'',
       user=''root'',
       password=''password'',
       database=''tut''
    )
    # Creating a cursor object
    cursorObj = connection.cursor()
    natural_language_search_query = ''''''
    SELECT * FROM Articles
    WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)
    AGAINST (''data set'' IN NATURAL LANGUAGE MODE)
    ''''''
    cursorObj.execute(natural_language_search_query)
    # Fetching all the results
    results = cursorObj.fetchall()
    # Display the result
    print("NATURAL LANGUAGE search results:")
    for row in results:
       print(row)
    cursorObj.close()
    connection.close()
    

    Output

    The output obtained is as shown below −

    NATURAL LANGUAGE search results:
    (4, ''Big Data Tutorial'', ''Big Data refers to data that has wider variety of data sets in larger numbers'')
    (1, ''MySQL Tutorial'', ''MySQL is a relational database system that uses SQL to structure data stored'')
    (3, ''Hadoop Tutorial'', ''Hadoop is framework that is used to process large sets of data'')
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MySQL – Boolean Fulltext Search nhận dự án làm có lương

    MySQL – Boolean Full-Text Search

    Table of content


    MySQL Boolean Full-Text Search

    The MySQL provides a full-text search functionality that supports three types of searches, one of which is the Boolean full-text search.

    This Boolean full-text search enables complex search operations on large amounts of text data, by allowing the use of Boolean operators such as (+, -, >, <, *, etc.) and search strings.

    Unlike the natural language full-text search, which searches for concepts, the Boolean full-text search in MySQL looks for specific words. To perform this type of search, it is necessary to include the IN BOOLEAN MODE modifier in the AGAINST expression.

    Syntax

    Following is the syntax to perform a Boolean full-text search using the IN BOOLEAN MODE modifier with the AGAINST expression in MySQL −

    SELECT column_name(s) FROM table_name
    WHERE MATCH(target_column_names)
    AGAINST(expression IN BOOLEAN MODE);
    

    Where,

    • The target_column_names are the names of the columns that we want to search the keyword in.
    • The expression is the list of keywords with the Boolean operators.

    MySQL Boolean Full-Text Search Operators

    The following table specifies the full-text search Boolean operators −

    Operator Description
    + Include, the word must be present.
    Exclude, the word must not be present.
    > Include, the word must be present, and have a higher priority.
    < Include, the word must be present, and have a lower priority.
    () Groups words into subexpressions

    Example

    First of all, let us create a table with the name ARTICLES using the following query −

    CREATE TABLE ARTICLES (
       ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
       ARTICLE_TITLE VARCHAR(100),
       DESCRIPTION TEXT,
       FULLTEXT (ARTICLE_TITLE, DESCRIPTION)
    );
    

    In the above query, we have defined full-text index on the columns ARTICLE_TITLE and DESCRIPTION. Now, let us insert values into the above-created table −

    INSERT INTO ARTICLES (ARTICLE_TITLE, DESCRIPTION) VALUES
    (''MySQL Tutorial'', ''MySQL is a relational database system that uses SQL to structure data stored''),
    (''Java Tutorial'', ''Java is an object-oriented and platform-independent programming languag''),
    (''Hadoop Tutorial'', ''Hadoop is framework that is used to process large sets of data''),
    (''Big Data Tutorial'', ''Big Data refers to data that has wider variety of data sets in larger numbers''),
    (''JDBC Tutorial'', ''JDBC is a Java based technology used for database connectivity'');
    

    The ARTICLES table is created as follows −

    ID ARTICLE_TITLE DESCRIPTION
    1 MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored
    2 Java Tutorial Java is an object-oriented and platform-independent programming language
    3 Hadoop Tutorial Hadoop is framework that is used to process large sets of data
    4 Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    5 JDBC Tutorial JDBC is a Java based technology used for database connectivity

    Now, let us perform the full-text search in Boolean mode, where we are searching for a row that contains the word ‘data’ −

    SELECT * FROM ARTICLES
    WHERE MATCH (ARTICLE_TITLE, DESCRIPTION)
    AGAINST(''data'' IN BOOLEAN MODE);
    

    Output

    As we can see in the output below, the above query returned three rows that contains the word ‘data’ −

    ID ARTICLE_TITLE DESCRIPTION
    4 Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    1 MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored
    3 Hadoop Tutorial Hadoop is framework that is used to process large sets of data

    Example

    In the following query, we are searching for the rows that contains the word ‘data’ but not ‘sets’ −

    SELECT * FROM ARTICLES
    WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)
    AGAINST(''+data -sets'' IN BOOLEAN MODE);
    

    Output

    The output for the query above is produced as given below −

    ARTICLE_TITLE DESCRIPTION
    MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored

    Example

    Here, we are searching for the rows that contain both the words ‘data’ and ‘set’ −

    SELECT * FROM ARTICLES
    WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)
    AGAINST(''+data +sets'' IN BOOLEAN MODE);
    

    Output

    On executing the given query, the output is displayed as follows −

    ID ARTICLE_TITLE DESCRIPTION
    4 Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    3 Hadoop Tutorial Hadoop is framework that is used to process large sets of data

    Example

    In the following query, we are searching for the rows that contains the word ‘set’ but not the higher rank for the rows that contain ‘set’ −

    SELECT * FROM ARTICLES
    WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)
    AGAINST(''+data sets'' IN BOOLEAN MODE);
    

    Output

    When we execute the query above, the output is obtained as follows −

    ID ARTICLE_TITLE DESCRIPTION
    4 Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    3 Hadoop Tutorial Hadoop is framework that is used to process large sets of data
    1 MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored

    Example

    Using the following query, we are searching for rows that contain the word ‘data’ and rank the particular record lower in the search, if it contains the word ‘tutorial’ −

    SELECT * FROM ARTICLES
    WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)
    AGAINST(''+data ~sets'' IN BOOLEAN MODE);
    

    Output

    On executing the given query, the output is displayed as follows −

    ID ARTICLE_TITLE DESCRIPTION
    4 Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers
    1 MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored
    3 Hadoop Tutorial Hadoop is framework that is used to process large sets of data

    Example

    Here, we are finding all the rows that contains words starting with ‘set’ −

    SELECT * FROM ARTICLES
    WHERE MATCH(ARTICLE_TITLE, DESCRIPTION)
    AGAINST(''set*'' IN BOOLEAN MODE);
    

    Output

    On executing the given query, the output is displayed as follows −

    ID ARTICLE_TITLE DESCRIPTION
    3 Hadoop Tutorial Hadoop is framework that is used to process large sets of data
    4 Big Data Tutorial Big Data refers to data that has wider variety of data sets in larger numbers

    MySQL Boolean Full-Text Search Features

    Following are some important features of MySQL Boolean full-text search −

    • In Boolean full-text search, MySQL does not sort the rows automatically by the relevance in descending order.
    • The InnoDB table requires all columns of the MATCH expression has a FULLTEXT index to perform Boolean queries.
    • If we provide multiple Boolean operators on a search query on InnoDB tables e.g. ”++hello”, MySQL does not support them and it generates an error. However, if we do the same thing in MyISAM, it ignores the extra operator and uses the operator that is closest to the search word.
    • Trailing (+) or (-) signs are not supported in InnoDB full-text search. It only supports leading + or − sign.
    • MySQL will generate an error if the search word is ”hello+” or ”hello-”. In addition to that, the following will also generate an error ”+*”, ”+-”.
    • MySQL will ignore the word in the search result, if it appears in more than 50% of the rows. This is called 50% threshold.

    Boolean Full-Text Search Using Client Program

    We can also perform Boolean Full-Text Search operation on a MySQL database using the client program.

    Syntax

    To perform the Boolean Full-Text Search through a PHP program, we need to execute the following SELECT statement using the mysqli function query() as follows −

    $sql = "SELECT * FROM articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION) AGAINST(''+data -sets'' IN BOOLEAN MODE)";
    $mysqli->query($sql);
    

    To perform the Boolean Full-Text Search through a JavaScript program, we need to execute the following SELECT statement using the query() function of mysql2 library as follows −

    sql = `SELECT * FROM articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION) AGAINST(''+data -sets'' IN BOOLEAN MODE)`;
    con.query(sql);
    

    To perform the Boolean Full-Text Search through a Java program, we need to execute the SELECT statement using the JDBC function executeQuery() as follows −

    String sql = "SELECT * FROM articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION) AGAINST(''+data -sets'' IN BOOLEAN MODE)";
    statement.executeQuery(sql);
    

    To perform the Boolean Full-Text Search through a python program, we need to execute the SELECT statement using the execute() function of the MySQL Connector/Python as follows −

    boolean_fulltext_search_query = ''select * from articles where MATCH (ARTICLE_TITLE, DESCRIPTION) AGAINST(''data'' IN BOOLEAN MODE)''
    cursorObj.execute(boolean_fulltext_search_query)
    

    Example

    Following are the programs −

    $dbhost = ''localhost
    $dbuser = ''root
    $dbpass = ''password
    $dbname = ''TUTORIALS
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error); exit(); } // printf(''Connected successfully.
    ''); //creating a table Articles that stores fulltext. $sql = "CREATE TABLE Articles (ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY, ARTICLE_TITLE VARCHAR(100), DESCRIPTION TEXT, FULLTEXT (ARTICLE_TITLE, DESCRIPTION))"; $result = $mysqli->query($sql); if ($result) { printf("Table created successfully...!n"); } //insert data $q = "INSERT INTO Articles (ARTICLE_TITLE, DESCRIPTION) VALUES (''MySQL Tutorial'', ''MySQL is a relational database system that uses SQL to structure data stored''), (''Java Tutorial'', ''Java is an object-oriented and platform-independent programming languag''), (''Hadoop Tutorial'', ''Hadoop is framework that is used to process large sets of data''), (''Big Data Tutorial'', ''Big Data refers to data that has wider variety of data sets in larger numbers''), (''JDBC Tutorial'', ''JDBC is a Java based technology used for database connectivity'')"; if ($res = $mysqli->query($q)) { printf("Data inserted successfully...!n"); } $s = "SELECT * FROM articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION) AGAINST(''+data -sets'' IN BOOLEAN MODE)"; if ($r = $mysqli->query($s)) { printf("Table Records: n"); while ($row = $r->fetch_assoc()) { printf(" ID: %d, Title: %s, Descriptions: %s", $row["id"], $row["ARTICLE_TITLE"], $row["DESCRIPTION"]); printf("n"); } } else { printf(''Failed''); } $mysqli->close();

    Output

    The output obtained is as shown below −

    Table created successfully...!
    Data inserted successfully...!
    Table Records:
    ID: 1, Title: MySQL Tutorial, Descriptions: MySQL is a relational database system that uses SQL to structure data stored
    
    var mysql = require("mysql2");
    var con = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "password",
    }); //Connecting to MySQL
    
    con.connect(function (err) {
      if (err) throw err;
      //   console.log("Connected successfully...!");
      //   console.log("--------------------------");
      sql = "USE TUTORIALS";
      con.query(sql);
    
      sql = "CREATE TABLE Articles (ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY, ARTICLE_TITLE VARCHAR(100), DESCRIPTION TEXT, FULLTEXT (ARTICLE_TITLE, DESCRIPTION) )";
      con.query(sql);
    
      //insert data into created table
      sql = `INSERT INTO Articles (ARTICLE_TITLE, DESCRIPTION) VALUES
      (''MySQL Tutorial'', ''MySQL is a relational database system that uses SQL to structure data stored''),
      (''Java Tutorial'', ''Java is an object-oriented and platform-independent programming languag''),
      (''Hadoop Tutorial'', ''Hadoop is framework that is used to process large sets of data''),
      (''Big Data Tutorial'', ''Big Data refers to data that has wider variety of data sets in larger numbers''),
      (''JDBC Tutorial'', ''JDBC is a Java based technology used for database connectivity'')`;
      con.query(sql);
    
      //display the table details!...
      sql = `SELECT * FROM articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION) AGAINST(''+data -sets'' IN BOOLEAN MODE)`;
      con.query(sql, function (err, result) {
        if (err) throw err;
        console.log(result);
      });
    });
    

    Output

    The output obtained is as shown below −

    [
      {
        id: 1,
        ARTICLE_TITLE: ''MySQL Tutorial'',
        DESCRIPTION: ''MySQL is a relational database system that uses SQL to structure data stored''
      }
    ]
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class BooleanFulltextSearch {
       public static void main(String[] args) {
          String url = "jdbc:mysql://localhost:3306/TUTORIALS";
          String username = "root";
          String password = "password";
          try {
             Class.forName("com.mysql.cj.jdbc.Driver");
             Connection connection = DriverManager.getConnection(url, username, password);
             Statement statement = connection.createStatement();
             System.out.println("Connected successfully...!");
    
             //creating a table that takes fulltext column...!
             String sql = "CREATE TABLE Articles (ID INT AUTO_INCREMENT NOT NULL PRIMARY KEY, ARTICLE_TITLE VARCHAR(100), DESCRIPTION TEXT, FULLTEXT (ARTICLE_TITLE, DESCRIPTION) )";
             statement.execute(sql);
             System.out.println("Table created successfully...!");
    
             //inserting data to the table
             String insert = "INSERT INTO Articles (ARTICLE_TITLE, DESCRIPTION) VALUES" +
             "(''MySQL Tutorial'', ''MySQL is a relational database system that uses SQL to structure data stored'')," +
             "(''Java Tutorial'', ''Java is an object-oriented and platform-independent programming languag'')," +
             "(''Hadoop Tutorial'', ''Hadoop is framework that is used to process large sets of data'')," +
             "(''Big Data Tutorial'', ''Big Data refers to data that has wider variety of data sets in larger numbers'')," +
             "(''JDBC Tutorial'', ''JDBC is a Java based technology used for database connectivity'')";
             statement.execute(insert);
             System.out.println("Data inserted successfully...!");
    
             //displaying the fulltext records in the boolean mode:
             ResultSet resultSet = statement.executeQuery("SELECT * FROM articles WHERE MATCH(ARTICLE_TITLE, DESCRIPTION) AGAINST(''+data -sets'' IN BOOLEAN MODE)");
             while (resultSet.next()){
                System.out.println(resultSet.getString(1)+" "+resultSet.getString(2)+ " "+resultSet.getString(3));
             }
             connection.close();
          } catch (Exception e) {
             System.out.println(e);
          }
       }
    }
    

    Output

    The output obtained is as shown below −

    Connected successfully...!
    Table created successfully...!
    Data inserted successfully...!
    1 MySQL Tutorial MySQL is a relational database system that uses SQL to structure data stored
    
    import mysql.connector
    # Establishing the connection
    connection = mysql.connector.connect(
        host=''localhost'',
        user=''root'',
        password=''password'',
        database=''tut''
    )
    # Creating a cursor object
    cursorObj = connection.cursor()
    boolean_fulltext_search_query = f"select * from articles where MATCH (ARTICLE_TITLE, DESCRIPTION) AGAINST(''data'' IN BOOLEAN MODE)"
    cursorObj.execute(boolean_fulltext_search_query)
    # Fetching all the results
    results = cursorObj.fetchall()
    # Display the result
    print("Boolean Fulltext search results:")
    for row in results:
        print(row)
    cursorObj.close()
    connection.close()
    

    Output

    The output obtained is as shown below −

    Boolean Fulltext search results:
    (4, ''Big Data Tutorial'', ''Big Data refers to data that has wider variety of data sets in larger numbers'')
    (1, ''MySQL Tutorial'', ''MySQL is a relational database system that uses SQL to structure data stored'')
    (3, ''Hadoop Tutorial'', ''Hadoop is framework that is used to process large sets of data'')
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MySQL – regexp_substr() Function nhận dự án làm có lương

    MySQL – REGEXP_SUBSTR() Function

    Table of content


    Regular expressions in MySQL are used in search operations to filter and retrieve records from a database table that match specified patterns.

    This process of detecting patterns in a set of data is known as pattern matching. It is helpful whenever the data is considered to have similar characteristics; in such cases, you might locate a pattern in the data and all its occurrences. Pattern matching is usually performed on raw data to make sure it is syntactically correct.

    Similarly, in MySQL, pattern matching is performed to check whether the data is correctly stored or not. If not, the incorrect pattern is detected (and sometimes replaced) using functions of regular expressions. The regexp_substr() function is used to detect specified patterns from a set of data.

    MySQL REGEXP_SUBSTR() Function

    The MySQL regexp_substr() function is used for pattern matching in a database. This function returns the substring of a string that matches the pattern specified, NULL if either there is no match or, the string or the pattern is NULL. Here, a pattern is defined as an extended regular expression or just an ordinary string.

    Syntax

    Following is the syntax of the MySQL regexp_substr() function −

    REGEXP_SUBSTR(expr, pattern[, pos[, occurrence[, match_type]]])
    

    Parameters

    The regexp_substr() function takes following parameter values −

    • expr: The string in which search is performed

    • pattern: The pattern that is searched in the string

    This method also accepts following optional arguments −

    • pos: Starting position of the search

    • occurrence: Which occurrence of a match to replace. If omitted, the default is 1 so it retrieves the first occurrence only.

    • match_type: A string that specifies how to perform matching.

    Example

    Following example shows the usage of MySQL regexp_substr() function on a simple string ”Welcome To Tutorialspoint!” using the SELECT statement as follows −

    SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''We'') AS RESULT;
    

    On executing the given query, the output is displayed as follows −

    Result
    We

    If the pattern is not present in the string, the result is returned as NULL. Let us try to search for the pattern ”Hi” in the same string ”Welcome To Tutorialspoint!” using the following query −

    SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''Hi'') AS RESULT;
    

    Following is the output −

    Result
    NULL

    Example

    Let us pass 5 as value to the ”pos” parameter so the search starts from the 5th position in the given string; and as we are passing the occurrence value as 2, the second occurrence of the pattern ”to” after 5th position will be retrieved, irrespective of its case −

    SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''To'', 5, 2, ''i'')
    AS RESULT;
    

    Output

    When we execute the above query, the output is obtained as follows −

    Result
    to

    Example

    If either of the first two arguments passed to this function is NULL, this function returns NULL. In the below query, we are passing NULL to the expression parameter.

    SELECT REGEXP_SUBSTR(NULL, ''value'');
    

    On executing the given program, the output is displayed as follows −

    REGEXP_SUBSTR(NULL, ”value”)
    NULL

    Here, we are passing NULL as a pattern to search −

    SELECT REGEXP_SUBSTR(''Welcome to Tutorialspoint'', NULL)
    AS Result;
    

    Following is the output −

    Result
    NULL

    Example

    In the following query, we are performing a search operation on a database table named CUSTOMERS using the REGEXP_SUBSTR() function. Firstly, let us create the table using the query below −

    CREATE TABLE CUSTOMERS (
       ID INT AUTO_INCREMENT,
       NAME VARCHAR(20) NOT NULL,
       AGE INT NOT NULL,
       ADDRESS CHAR (25),
       SALARY DECIMAL (18, 2),
       PRIMARY KEY (ID)
    );
    

    Following query adds 7 records into the above-created table −

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
    (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ),
    (2, ''Khilan'', 25, ''Delhi'', 1500.00 ),
    (3, ''Kaushik'', 23, ''Kota'', 2000.00 ),
    (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ),
    (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ),
    (6, ''Komal'', 22, ''Hyderabad'', 4500.00 ),
    (7, ''Muffy'', 24, ''Indore'', 10000.00 );
    

    Execute the following query to display all the records of CUSTOMERS table −

    Select * from CUSTOMERS;
    

    Following is the CUSTOMERS table −

    ID NAME AGE ADDRESS SALARY
    1 Ramesh 32 Ahmedabad 2000.00
    2 Khilan 25 Delhi 1500.00
    3 Kaushik 23 Kota 2000.00
    4 Chaitali 25 Mumbai 6500.00
    5 Hardik 27 Bhopal 8500.00
    6 Komal 22 Hyderabad 4500.00
    7 Muffy 24 Indore 10000.00

    Now, we are using the REGEXP_SUBSTR() function to retrieve the substring from the the NAME column values that begin with ”Ra”.

    SELECT REGEXP_SUBSTR(NAME, ''^Ra'') AS RESULT FROM CUSTOMERS;
    

    Output

    As we can see the output below, only the first record in NAME column has a substring ”Ra” in it −

    Result
    Ra
    NULL
    NULL
    NULL
    NULL
    NULL
    NULL

    REGEXP_SUBSTR() Funcion Using a Client Program

    We can also perform the MySQL REGEXP_SUBSTR() function using the client programs to detect specified patterns from a set of data.

    Syntax

    Following are the syntaxes of this operation in various programming languages −

    To retrieve a part of a string that matches a specific pattern from MySQL database through PHP program, we need to execute the ”SELECT” statement using the mysqli function query() as follows −

    $sql = "SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''We'') AS RESULT";
    $mysqli->query($sql);
    

    To retrieve a part of a string that matches a specific pattern from MySQL database through Node.js program, we need to execute the ”SELECT” statement using the query() function of the mysql2 library as follows −

    sql = "SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''We'') AS RESULT";
    con.query(sql);
    

    To retrieve a part of a string that matches a specific pattern from MySQL database through Java program, we need to execute the ”SELECT” statement using the JDBC function executeUpdate() as follows −

    String sql = "SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''We'') AS RESULT";
    statement.executeQuery(sql);
    

    To retrieve a part of a string that matches a specific pattern from MySQL database through Python program, we need to execute the ”SELECT” statement using the execute() function of the MySQL Connector/Python as follows −

    sql = "SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''We'') AS RESULT"
    cursorObj.execute(sql)
    

    Example

    Following are the programs −

    $dbhost = ''localhost
    $dbuser = ''root
    $dbpass = ''password
    $db = ''TUTORIALS
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db);
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error); exit(); } //printf(''Connected successfully.
    ''); $sql = "SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''We'') AS RESULT"; if($result = $mysqli->query($sql)){ while($row = mysqli_fetch_array($result)){ printf("Result: %s", $row[''RESULT'']); } } if($mysqli->error){ printf("Error message: ", $mysqli->error); } $mysqli->close();

    Output

    The output obtained is as shown below −

    Result: We
    
    var mysql = require(''mysql2'');
    var con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"password"
    });
     //Connecting to MySQL
     con.connect(function(err) {
     if (err) throw err;
      //console.log("Connected successfully...!");
      //console.log("--------------------------");
     sql = "USE TUTORIALS";
     con.query(sql);
     sql = "SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''We'') AS RESULT";
     console.log("Select query executed successfully..!");
     console.log("Table records: ");
     con.query(sql);
     con.query(sql, function(err, result){
     if (err) throw err;
     console.log(result);
     });
    });
    

    Output

    The output obtained is as shown below −

    Select query executed successfully..!
    Table records:
    [ { RESULT: ''We'' } ]
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class regexp_substr {
        public static void main(String[] args) {
            String url = "jdbc:mysql://localhost:3306/TUTORIALS";
            String user = "root";
            String password = "password";
            ResultSet rs;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user, password);
                Statement st = con.createStatement();
                //System.out.println("Database connected successfully...!");
                String sql = "SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''We'') AS RESULT";
                rs = st.executeQuery(sql);
                while(rs.next()) {
                    String result = rs.getString("RESULT");
                    System.out.println("Result: " + result);
                }
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Output

    The output obtained is as shown below −

    Result: We
    
    import mysql.connector
    # Establishing the connection
    connection = mysql.connector.connect(
        host=''localhost'',
        user=''root'',
        password=''password'',
        database=''tut''
    )
    # Creating a cursor object
    cursorObj = connection.cursor()
    regexp_substr_query = f"SELECT REGEXP_SUBSTR(''Welcome To Tutorialspoint!'', ''We'') AS RESULT"
    cursorObj.execute(regexp_substr_query)
    # Fetching all the results
    results = cursorObj.fetchall()
    # Display the result
    print("Result of REGEXP_SUBSTR() Function:")
    for row in results:
        result = row[0]
        print(f"The extracted substring is: ''{result}''")
    cursorObj.close()
    connection.close()
    

    Output

    The output obtained is as shown below −

    Result of REGEXP_SUBSTR() Function:
    The extracted substring is: ''We''
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MySQL – Fulltext Search nhận dự án làm có lương

    MySQL – Full-Text Search

    Table of content


    The MySQL Full-Text Search allows us to search for a text-based data, stored in the database. Before performing the full-text search in a column(s) of table, we must create a full-text index on those columns.

    The FTS (full-text search) provides the capability to match the searched string value through large text content such as blogs, articles, etc.

    To perform a Full-Text Search on a MySQL table, we use MATCH() and AGAINST() functions in a WHERE clause of an SQL SELECT statement.

    Stop words are words that are commonly used (such as ”on”, ”the”, or, ”it”) in sentences and will be ignored during the searching process.

    The basic syntax to perform a full-text search on a MySQL is as follows −

    SELECT column_name(s) FROM table_name
    WHERE MATCH(col1, col2, ...)
    AGAINST(expression [search_modifier])
    

    Here,

    • MATCH() function contains one or more columns separated by commas to be searched.
    • AGAINST() function contains a search string to use for the full-text search.

    Key Points of MySQL Full-Text Search

    Following are some key points about the full-text search in MySQL −

    • Either InnoDB or MyISAM tables use the full-text indexes. The minimum length of the word for full-text searches is three characters for InnoDB tables and four characters for MyISAM tables.
    • Full-Text indexes can be created on text-based columns (CHAR, VARCHAR or TEXT columns).
    • A FULLTEXT index can be defined while creating the table using CREATE TABLE statement or can be defined later using the ALTER TABLE or CREATE INDEX statements.
    • Without FULLTEXT index, it is faster to load large data sets into a table than to load data into a table which has an existing FULLTEXT index. Therefore it is recommended to create the index after loading data.

    Types of Full-Text Searches

    There are three types of full-text searches. The same is described below:

    • Natural Language Full-Text Searches: This allows the user to enter the search query in a natural human language without any special characters or operators. The search engine will examine the query entered by the user and returns the relevant results based on the user”s intent.
    • Boolean Full-Text Searches: This allows us to perform a full-text search based on very complex queries in the Boolean mode along with Boolean operators such as +, -, >,
    • Query Expansion Searches: This expands the user”s query to widen the search result of the full-text searches based on automatic relevance feedback or blind query expansion.

    Creating MySQL FULLTEXT Index

    In MySQL, we can define a full-text index on particular column while creating a new table or on an existing table. This can be done in three ways:

    • Using the FULLTEXT Keyword

    • Using the ALTER TABLE Statement

    • Using the CREATE INDEX Statement

    Using the FULLTEXT Keyword

    To define full-text index on a column while creating a new table, we use the FULLTEXT keyword on that column within the CREATE TABLE query. Following is the syntax −

    CREATE TABLE table_name(
       column1 data_type,
       column2 data_type,
       ...,
       FULLTEXT (column1, column2, ...)
    );
    
    Example

    Let us create first a table named FILMS and define the full-text index on NAME and DIRECTOR columns, using the following query −

    CREATE TABLE FILMS (
       ID int auto_increment not null primary key,
       NAME varchar(50),
       DIRECTOR TEXT,
       FULLTEXT (NAME, DIRECTOR)
    );
    

    Now, let us insert values into this table using the following query −

    INSERT INTO FILMS (NAME, DIRECTOR) VALUES
    (''RRR'', ''Directed by Rajamouli''),
    (''Bahubali'', ''Directed by Rajamouli''),
    (''Avatar'', ''Directed by James cameron''),
    (''Robot'', ''Directed by Shankar'');
    

    The table will be created as −

    ID NAME DIRECTOR
    1 RRR Directed by Rajamouli
    2 Bahubali Directed by Rajamouli
    3 Avatar Directed by James Cameron
    4 Robot Directed by Shankar

    Here, we are fetching all the rows from the FILMS table where the NAME or DIRECTOR column matches the string ‘Rajamouli’ using the MATCH and AGAINST functions as shown below −

    SELECT * FROM FILMS
    WHERE MATCH (NAME, DIRECTOR)
    AGAINST (''Rajamouli'');
    
    Output

    As we can see in the output below, the full-text search has been performed against a string ‘Rajamouli’ and it returned the rows which contains this string.

    ID NAME DIRECTOR
    1 RRR Directed by Rajamouli
    2 Bahubali Directed by Rajamouli

    Using the ALTER TABLE Statement

    In MySQL, we can create full-text index on particular columns of an existing table using the ALTER TABLE statement. Following is the syntax −

    ALTER TABLE table_name
    ADD FULLTEXT (column1, column2,...)
    
    Example

    In this example, we are defining a full-text index named FULLTEXT on NAME and DIRECTOR columns of the previously created FILMS table −

    ALTER TABLE FILMS ADD FULLTEXT (NAME, DIRECTOR);
    

    Now, let us retrieve all the rows from the FILMS table where the NAME or DIRECTOR column matches the string ”Shankar”.

    SELECT * FROM FILMS
    WHERE MATCH (NAME, DIRECTOR)
    AGAINST (''Shankar'');
    
    Output

    Following is the output −

    ID NAME DIRECTOR
    4 Robot Directed by Shankar

    Using the CREATE INDEX Statement

    In MySQL, we can also create a full-text index for an existing table using the CREATE INDEX statement. Following is the syntax −

    CREATE FULLTEXT INDEX index_name
    ON table_name (index_column1, index_column2,...)
    
    Example

    We are creating a full-text index with the name INDEX_FULLTEXT on the NAME and DIRECTOR column of the FILMS table −

    CREATE FULLTEXT INDEX INDEX_FULLTEXT ON FILMS (NAME, DIRECTOR);
    

    Now, let us retrieve all the rows from the FILMS table where the NAME or DIRECTOR column matches the string value as shown in the below query −

    SELECT * FROM FILMS
    WHERE MATCH(NAME, DIRECTOR)
    AGAINST (''James Cameron'');
    
    Output

    Following is the output −

    ID NAME DIRECTOR
    3 Avatar Directed by James Cameron

    Dropping MySQL FULLTEXT index

    In MySQL, we can remove or drop a full-text index from a table using the ALTER TABLE DROP INDEX statement.

    Syntax

    Following is the syntax −

    ALTER TABLE table_name DROP INDEX index_name;
    
    Example

    In the following query, we will delete the previously created full-text index −

    ALTER TABLE FILMS DROP INDEX INDEX_FULLTEXT;
    
    Verification

    Let us verify whether the index is dropped or not by executing the below query −

    SELECT * FROM FILMS
    WHERE MATCH(NAME, DIRECTOR)
    AGAINST (''James Cameron'');
    

    As we can see in the output, the full-text index is removed on the NAME and DIRECTOR columns.

    ERROR 1191 (HY000): Can''t find FULLTEXT index matching the column list
    

    Full-Text Search Using Client Program

    In addition to performing the full-text search using MySQL Query, we can also do so using the client program.

    Syntax

    To perform the Fulltext Search on a MySQL database through a PHP program, we need to execute the CREATE TABLE statement using the mysqli function query() as follows −

    $sql = "CREATE TABLE FILMS (ID int auto_increment not null primary key, NAME varchar(50), DIRECTOR TEXT, FULLTEXT (NAME, DIRECTOR) )";
    $mysqli->query($sql);
    

    To perform the Fulltext Search on a MySQL database through a JavaScript program, we need to execute the CREATE TABLE statement using the query() function of mysql2 library as follows −

    sql = "CREATE TABLE FILMS (ID int auto_increment not null primary key, NAME varchar(50), DIRECTOR TEXT, FULLTEXT (NAME, DIRECTOR) )";
    con.query(sql);
    

    To perform the Fulltext Search on a MySQL database through a Java program, we need to execute the CREATE TABLE statement using the JDBC function execute() as follows −

    String sql = "CREATE TABLE FILMS (ID int auto_increment not null primary key, NAME varchar(50), DIRECTOR TEXT, FULLTEXT (NAME, DIRECTOR) )";
    statement.execute(sql);
    

    To perform the Fulltext Search on a MySQL database through a python program, we need to execute the CREATE TABLE statement using the execute() function of the MySQL Connector/Python as follows −

    fulltext_search_query = "SELECT * FROM FILMS WHERE MATCH (NAME, DIRECTOR) AGAINST (''Rajamouli'')"
    cursorObj.execute(fulltext_search_query)
    

    Example

    Following are the programs −

    $dbhost = ''localhost
    $dbuser = ''root
    $dbpass = ''password
    $dbname = ''TUTORIALS
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error); exit(); } // printf(''Connected successfully.
    ''); //creating a table films that stores fulltext. $sql = "CREATE TABLE FILMS (ID int auto_increment not null primary key, NAME varchar(50), DIRECTOR TEXT, FULLTEXT (NAME, DIRECTOR) )"; $result = $mysqli->query($sql); if ($result) { printf("Table created successfully...!n"); } //insert data $q = "INSERT INTO FILMS (NAME, DIRECTOR) VALUES (''RRR'', ''The film RRR is directed by Rajamouli''), (''Bahubali'', ''The film Bahubali is directed by Rajamouli''), (''Avatar'', ''The film Avatar is directed by James cameron''), (''Robot'', ''The film Robot is directed by Shankar'')"; if ($res = $mysqli->query($q)) { printf("Data inserted successfully...!n"); } //now display the table records $s = "SELECT * FROM FILMS WHERE MATCH (NAME, DIRECTOR) AGAINST (''Rajamouli'')"; if ($r = $mysqli->query($s)) { printf("Table Records: n"); while ($row = $r->fetch_assoc()) { printf(" ID: %d, Name: %s, Director: %s", $row["ID"], $row["NAME"], $row["DIRECTOR"]); printf("n"); } } else { printf(''Failed''); } $mysqli->close();

    Output

    The output obtained is as shown below −

    Table created successfully...!
    Data inserted successfully...!
    Table Records:
     ID: 1, Name: RRR, Director: The film RRR is directed by Rajamouli
     ID: 2, Name: Bahubali, Director: The film Bahubali is directed by Rajamouli
    
    var mysql = require("mysql2");
    var con = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "password",
    }); //Connecting to MySQL
    
    con.connect(function (err) {
      if (err) throw err;
      //   console.log("Connected successfully...!");
      //   console.log("--------------------------");
      sql = "USE TUTORIALS";
      con.query(sql);
    
      sql = "CREATE TABLE FILMS (ID int auto_increment not null primary key, NAME varchar(50), DIRECTOR TEXT, FULLTEXT (NAME, DIRECTOR) )";
      con.query(sql);
    
      //insert data into created table
      sql = `INSERT INTO FILMS (NAME, DIRECTOR)
      VALUES (''RRR'', ''The film RRR is directed by Rajamouli''),
      (''Bahubali'', ''The film Bahubali is directed by Rajamouli''),
      (''Avatar'', ''The film Avatar is directed by James cameron''),
      (''Robot'', ''The film Robot is directed by Shankar'')`;
      con.query(sql);
    
      //display the table details!...
      sql = `SELECT * FROM FILMS WHERE MATCH (NAME, DIRECTOR) AGAINST (''Rajamouli'')`;
      con.query(sql, function (err, result) {
        if (err) throw err;
        console.log(result);
      });
    });
    

    Output

    The output obtained is as shown below −

    [
      {
        ID: 1,
        NAME: ''RRR'',
        DIRECTOR: ''The film RRR is directed by Rajamouli''
      },
      {
        ID: 2,
        NAME: ''Bahubali'',
        DIRECTOR: ''The film Bahubali is directed by Rajamouli''
      }
    ]
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class FulltextSearch {
       public static void main(String[] args) {
          String url = "jdbc:mysql://localhost:3306/TUTORIALS";
          String username = "root";
          String password = "password";
          try {
             Class.forName("com.mysql.cj.jdbc.Driver");
             Connection connection = DriverManager.getConnection(url, username, password);
             Statement statement = connection.createStatement();
             System.out.println("Connected successfully...!");
    
             //creating a table that takes fulltext column...!
             String sql = "CREATE TABLE FILMS (ID int auto_increment not null primary key, NAME varchar(50), DIRECTOR TEXT, FULLTEXT (NAME, DIRECTOR) )";
             statement.execute(sql);
             System.out.println("Table created successfully...!");
    
             //inserting data to the tables
             String insert = "INSERT INTO FILMS (NAME, DIRECTOR) VALUES (''RRR'', ''The film RRR is directed by Rajamouli''), (''Bahubali'', ''The film Bahubali is directed by Rajamouli'')," +
                     "(''Avatar'', ''The film Avatar is directed by James cameron''), (''Robot'', ''The film Robot is directed by Shankar'')";
             statement.execute(insert);
             System.out.println("Data inserted successfully...!");
    
             //displaying the table records...!
             ResultSet resultSet = statement.executeQuery("SELECT * FROM FILMS WHERE MATCH (NAME, DIRECTOR) AGAINST (''Rajamouli'')");
             while (resultSet.next()){
                System.out.println(resultSet.getString(1)+" "+resultSet.getString(2)+ " "+resultSet.getString(3));
             }
             connection.close();
          } catch (Exception e) {
             System.out.println(e);
          }
       }
    }
    

    Output

    The output obtained is as shown below −

    Connected successfully...!
    Table created successfully...!
    Data inserted successfully...!
    1 RRR The film RRR is directed by Rajamouli
    2 Bahubali The film Bahubali is directed by Rajamouli
    
    import mysql.connector
    # Establishing the connection
    connection = mysql.connector.connect(
        host=''localhost'',
        user=''root'',
        password=''password'',
        database=''tut''
    )
    # Creating a cursor object
    cursorObj = connection.cursor()
    fulltext_search_query = f"SELECT * FROM FILMS WHERE MATCH (NAME, DIRECTOR) AGAINST (''Rajamouli'');"
    cursorObj.execute(fulltext_search_query)
    # Fetching all the results
    results = cursorObj.fetchall()
    # Display the result
    print("Full-text search results:")
    for row in results:
        print(row)
    cursorObj.close()
    connection.close()
    

    Output

    The output obtained is as shown below −

    Full-text search results:
    (1, ''RRR'', ''The film RRR is directed by Rajamouli'')
    (2, ''Bahubali'', ''The film Bahubali is directed by Rajamouli'')
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MySQL – regexp_replace() Function nhận dự án làm có lương

    MySQL – REGEXP_REPLACE() Function

    Table of content


    Regular expressions in MySQL are used in search operations to not only filter records but also replace the pattern occurrences in a string.

    Consider a scenario where you noticed a spelling error among the huge sets of data present in a MySQL database. Now, you are supposed to correct all occurrences of these errors in this database without disturbing the other data. This is where regular expressions are extremely advantageous.

    You can use regular expressions to find the accurate occurrences of the same error and replace it with the right characters. This is done using the regexp_replace() function.

    MySQL REGEXP_REPLACE() Function

    The MySQL regexp_replace() function is used to find and replace occurrences of a string that match specific patterns. If there”s a match, it replaces the string with another. If there”s no match, it returns the original string. If the string or pattern is NULL, it returns NULL. You can use a regular expression or a simple string as the pattern in this function.

    Syntax

    Following is the syntax of the MySQL regexp_replace() function −

    REGEXP_REPLACE(expr, pattern, repl[, pos[, occurrence[, match_type]]])
    

    Parameters

    The regexp_replace() function takes following parameter values −

    • expr: The string in which search is performed

    • pattern: The pattern that is searched in the string

    • repl: The replacement string

    This method also accepts following optional arguments −

    • pos − Starting position of the search

    • occurrence − Which occurrence of a match to replace. If omitted, the default is 0 so it replaces all occurrences.

    • match_type − A string that specifies how to perform matching.

    Example

    In the following query, we are performing a search operation on a simple string using the MySQL REGEXP_REPLACE() function −

    SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''Welcome'', ''Welll'')
    AS RESULT;
    

    As we can observe the output below, the string ”Welcome” is found and replaced with ”Welll” −

    RESULT
    Welll To Tutorialspoint!

    But if the pattern is not found in the string, the original string is displayed by the function. Look at the following query −

    SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''H'', ''Hi'') AS RESULT;
    

    On executing the given query, the output is displayed as follows −

    RESULT
    Welcome To Tutorialspoint!

    Example

    Let us also try to pass optional arguments to this function as case-insensitive matching(i). Here, the search starts from the 10th position in the given string; and as we are passing the occurrence value as 1, only the first occurrence of the letter ”t” after 10th position will be replaced irrespective of its case −

    SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''t'', ''x'', 10, 1, ''i'') AS RESULT;
    

    Output

    The output for the program above is produced as given below −

    RESULT
    Welcome To xutorialspoint!

    Example

    The following query replaces all the occurrences of the string “is” in the given text −

    SELECT REGEXP_REPLACE(''This is a sample string'', ''is'', ''@@@@'')
    As Result;
    

    Output

    On executing the given query, the output is displayed as follows −

    RESULT
    Th@@@@ @@@@ a sample string

    Example

    The following query replaces only the first occurrence of the string “This” in the given text with “That” −

    SELECT REGEXP_REPLACE(''This is a test and This is another test'', ''^This'', ''That'')
    As Result;
    

    Output

    The output for the query above is produced as given below −

    RESULT
    That is a test and This is another test

    Example

    Here, the below query replace the words ”wall” or ”floor” with the word ”bed” in the given string using the MySQL REGEXP_REPLACE() function −

    SELECT REGEXP_REPLACE (''Humpty dumpty sat on a wall and slept on the floor'', ''wall|floor'', ''bed'') As Result;
    

    Output

    On executing the given program, the output is displayed as follows −

    RESULT
    Humpty dumpty sat on a bed and slept on the bed

    Example

    The following query replaces the first occurrence of the string “eat” with the string “drink” in the provided input string.

    In the query, the fourth parameter “1” specifies the position to start the search and the fifth parameter “1” is the number of replacements to be made. Therefore, only the first occurrence of “eat” is replaced with “drink”.

    SELECT REGEXP_REPLACE(''eat sleep repeat and eat'', ''eat'', ''drink'', 1, 1)
    As Result;
    

    Output

    Following is the output −

    RESULT
    drink sleep repeat and eat

    Example

    If either of the first two arguments passed to this function is NULL, this function returns NULL. Here, we are passing NULL to the string parameter.

    SELECT REGEXP_REPLACE(NULL, ''value'', ''test'') As Result;
    

    Following is the output −

    Result
    NULL

    If we pass NULL to the pattern parameter, it returns NULL as output.

    SELECT REGEXP_REPLACE(''Welcome to Tutorialspoint'', NULL, ''sample'')
    As Result;
    

    The output for the query above is produced as given below −

    Result
    NULL

    If you pass empty string as the replacement string, this function returns NULL.

    SELECT REGEXP_REPLACE(''Welcome to Tutorialspoint'', NULL, '''')
    As Result;
    

    On executing the given query, the output is displayed as follows −

    Result
    NULL

    Example

    In another example, let us try to perform a search operation on a database table named CUSTOMERS using the REGEXP_REPLACE() function. First of all, let us create the table using the following query −

    CREATE TABLE CUSTOMERS (
       ID INT AUTO_INCREMENT,
       NAME VARCHAR(20) NOT NULL,
       AGE INT NOT NULL,
       ADDRESS CHAR (25),
       SALARY DECIMAL (18, 2),
       PRIMARY KEY (ID)
    );
    

    The following query inserts 7 records into the above created table −

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
    (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ),
    (2, ''Khilan'', 25, ''Delhi'', 1500.00 ),
    (3, ''Kaushik'', 23, ''Kota'', 2000.00 ),
    (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ),
    (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ),
    (6, ''Komal'', 22, ''Hyderabad'', 4500.00 ),
    (7, ''Muffy'', 24, ''Indore'', 10000.00 );
    

    Execute the following SELECT statement to display all the records of CUSTOMERS table −

    Select * from CUSTOMERS;
    

    Following is the CUSTOMERS table −

    ID NAME AGE ADDRESS SALARY
    1 Ramesh 32 Ahmedabad 2000.00
    2 Khilan 25 Delhi 1500.00
    3 Kaushik 23 Kota 2000.00
    4 Chaitali 25 Mumbai 6500.00
    5 Hardik 27 Bhopal 8500.00
    6 Komal 22 Hyderabad 4500.00
    7 Muffy 24 Indore 10000.00

    The following query uses the REGEXP_REPLACE() function to update the NAME column in the person_tbl table. It looks for names that start with the letter ”A” and replaces that ”A” with ”An”.

    SELECT REGEXP_REPLACE(NAME, ''^A'', ''An'') AS Result FROM CUSTOMERS;
    

    On executing the given query, the output is displayed as follows −

    Result
    Ramesh
    Khilan
    Kaushik
    Chaitali
    Hardik
    Komal
    Muffy

    But if the pattern is not found in any record of the table, the original values of the table are displayed by the function. Look at the following query −

    SELECT REGEXP_REPLACE(ADDRESS, ''^Z'', ''P'') AS RESULT FROM CUSTOMERS;
    

    There is no record in ADDRESS column that starts with letter ”Z”. So, it returned the original records as output −

    Result
    Ahmedabad
    Delhi
    Kota
    Mumbai
    Bhopal
    Hyderabad
    Indore

    The following query is using the REGEXP_REPLACE function to replace the second occurrence of the letter ”r” with ”R” in the ADDRESS column of the CUSTOMERS table −

    SELECT REGEXP_REPLACE(ADDRESS, ''r'', ''R'', 2, 0, ''c'')
    AS RESULT FROM CUSTOMERS;
    

    As we can see in the output, the records ”Hyderabad” and ”Indore” has letter ”r” in it. And they are replaced by ”R” −

    Result
    Ahmedabad
    Delhi
    Kota
    Mumbai
    Bhopal
    HydeRabad
    IndoRe

    REGEXP_REPLACE() Funcion Using a Client Program

    We can also perform the MySQL REGEXP_REPLACE function using the client programs to find and replace occurrences of a string that match specific patterns.

    Syntax

    Following are the syntaxes of this operation in various programming languages −

    To match with specific pattern and replace with another string using MySQL Query through PHP program, we need to execute the ”SELECT” statement using the mysqli function query() as follows −

    $sql = "SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''Welcome'', ''Welcom'')";
    $mysqli->query($sql);
    

    To match with specific pattern and replace with another string using MySQL Query through Node.js program, we need to execute the ”SELECT” statement using the query() function of the mysql2 library as follows −

    sql = "SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''Welcome'', ''Welcom'')";
    con.query(sql);
    

    To match with specific pattern and replace with another string using MySQL Query through Java program, we need to execute the ”SELECT” statement using the JDBC function executeUpdate() as follows −

    String sql = "SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''Welcome'', ''Welcom'')";
    statement.executeQuery(sql);
    

    To match with specific pattern and replace with another string using MySQL Query through Python program, we need to execute the ”SELECT” statement using the execute() function of the MySQL Connector/Python as follows −

    sql = "SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''Welcome'', ''Welcom'')"
    cursorObj.execute(sql)
    

    Example

    Following are the programs −

    $dbhost = ''localhost
    $dbuser = ''root
    $dbpass = ''password
    $db = ''TUTORIALS
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db);
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error); exit(); } //printf(''Connected successfully.
    ''); $sql = "SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''Welcome'', ''Welcom'') AS RESULT"; if($result = $mysqli->query($sql)){ while($row = mysqli_fetch_array($result)){ printf("Result: %s", $row[''RESULT'']); } } if($mysqli->error){ printf("Error message: ", $mysqli->error); } $mysqli->close();

    Output

    The output obtained is as shown below −

    Result: Welcom To Tutorialspoint!
    
    var mysql = require(''mysql2'');
    var con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"password"
    });
     //Connecting to MySQL
     con.connect(function(err) {
     if (err) throw err;
      //console.log("Connected successfully...!");
      //console.log("--------------------------");
     sql = "USE TUTORIALS";
     con.query(sql);
     sql = "SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''Welcome'', ''Welcom'') AS RESULT";
     console.log("Select query executed successfully..!");
     console.log("Table records: ");
     con.query(sql);
     con.query(sql, function(err, result){
     if (err) throw err;
     console.log(result);
     });
    });
    

    Output

    The output obtained is as shown below −

    Select query executed successfully..!
    Table records:
    [ { RESULT: ''Welcome To Tutorialspoint!'' } ]
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class regexp_replace {
        public static void main(String[] args) {
            String url = "jdbc:mysql://localhost:3306/TUTORIALS";
            String user = "root";
            String password = "password";
            ResultSet rs;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user, password);
                Statement st = con.createStatement();
                //System.out.println("Database connected successfully...!");
                String sql = "SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''Welcome'', ''Welcom'') AS RESULT";
                rs = st.executeQuery(sql);
                while(rs.next()) {
                    String result = rs.getString("RESULT");
                    System.out.println("Result: " + result);
                }
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Output

    The output obtained is as shown below −

    Result: Welcom To Tutorialspoint!
    
    import mysql.connector
    # Establishing the connection
    connection = mysql.connector.connect(
        host=''localhost'',
        user=''root'',
        password=''password'',
        database=''tut''
    )
    # Creating a cursor object
    cursorObj = connection.cursor()
    regexp_replace_query = f"SELECT REGEXP_REPLACE(''Welcome To Tutorialspoint!'', ''Welcome'', ''Welcom'') AS RESULT;"
    cursorObj.execute(regexp_replace_query)
    # Fetching all the results
    results = cursorObj.fetchall()
    # Display the result
    print("Result of REGEXP_REPLACE() Function:")
    for row in results:
        result = row[0]
        print(f"The modified string is: ''{result}''")
    cursorObj.close()
    connection.close()
    

    Output

    The output obtained is as shown below −

    Result of REGEXP_REPLACE() Function:
    The modified string is: ''Welcom To Tutorialspoint!''
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MySQL – regexp_instr() Function nhận dự án làm có lương

    MySQL – REGEXP_INSTR() Function

    Table of content


    MySQL supports various types of pattern matching operations to retrieve filtered result-sets from huge database tables. But, pattern matching with regular expressions is a powerful way to perform a complex search.

    A regular expression is technically defined as a sequence of characters that represent a pattern in an input text. It is used to locate or replace text strings using some patterns; this pattern can either be a single character, multiple characters or words, etc.

    These regular expressions in MySQL provide various functions and operators to easily perform the search operations. One such function is regexp_instr() function.

    MySQL REGEXP_INSTR() Function

    The MySQL regexp_instr() function is used to match specified patterns with either a string or the data in database tables. This function returns the starting index of the substring of a string that matches the specified pattern, returns 0 if there is no match, or NULL if the string or the pattern is NULL. Character indices of this string starts at 1.

    Syntax

    Following is the syntax of the MySQL regexp_instr() function −

    REGEXP_INSTR(expr, pattern[, pos[, occurrence[, return_option[, match_type]]]])
    

    Where expr is the string in which the search is to be performed and pat is the pattern/regular expression that is to be searched. In addition to the expression and string values this method accepts the following optional parameters.

    Parameters

    The regexp_instr() function takes following parameter values −

    • expr: The string in which search is performed

    • pattern: The pattern that is searched in the string

    Following are the optional arguments that can be passed to this function −

    • pos: The position in expr at which to start the search. If omitted, the default is 1.

    • occurrence: Which occurrence of a match to search for. If omitted, the default is 1.

    • return_option: Which type of position to return. If this value is 0, REGEXP_INSTR() returns the position of the matched substring”s first character. If this value is 1, REGEXP_INSTR() returns the position following the matched substring. If omitted, the default is 0.

    • match_type:This is a string which consists of various characters representing the desired features of the match this may contain one or all of these characters. Following are various characters using which you can specify the match type.

      • c This character indicates the case-sensitive matching.

      • i This character indicates the case-insensitive matching.

      • m This character indicates the i.e., multiple lines (separated by line terminators) with in a single string are recognized.

      • n If this character is specified, the dot (.) character matches line terminators.

      • u If this character is specified, only new line character is recognized as line ending by ., ^, and $.

    Example

    In this example, we are performing a search operation on a simple string using the MySQL REGEXP_INSTR() function −

    SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''To'') AS RESULT;
    

    The pattern ”To” is found at 9th index −

    Result
    9

    If there is no match found in the string, the return value will be ”0” −

    SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''xx'') AS RESULT;
    

    Following is the output −

    Result
    0

    Example

    Let us also pass optional arguments to this function and observe the result. Here, the search search position starts at 5 to find the 2nd occurrence of ”T” after that position. As the return option is set to 1, the position following the match is returned. −

    SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''T'', 5, 2, 1) AS RESULT;
    

    Output

    Following is the output −

    Result
    13

    Example

    The following query searches for the position for any alphabetic character in the provided string ”9848032919”. If found, it returns 1. Else, 0.

    SELECT REGEXP_INSTR(''9848032919'', ''[[:alpha:]]'');
    

    Output

    Executing the query above will produce the following output −

    REGEXP_INSTR(”9848032919”, ”[[:alpha:]]”)
    0

    Example

    The below query searches for the position of either ”town” or ”city” in the provided string −

    SELECT REGEXP_INSTR(''Vishakapatnam is city of destiny '', ''town|city'')
    As Result;
    

    Output

    The output for the above query is produced as given below −

    Result
    0

    Example

    If either of the first two arguments passed to this function is NULL, this function returns NULL. Here, we are passing ”NULL” as search pattern.

    SELECT REGEXP_INSTR(''Tutorialspoint'', NULL)
    As Result;
    

    If we compile and run the query, the result is produced as follows −

    Result
    NULL

    In the following query, we are passing ”NULL” to the string parameter.

    SELECT REGEXP_INSTR(NULL, ''to'')
    As Result;
    

    When we execute the query above, the output is obtained as follows −

    Result
    NULL

    Example

    In another example, let us perform a search operation on a database table named CUSTOMERS using the REGEXP_INSTR() function. Firstly, let us create the table using the following query −

    CREATE TABLE CUSTOMERS (
       ID INT AUTO_INCREMENT,
       NAME VARCHAR(20) NOT NULL,
       AGE INT NOT NULL,
       ADDRESS CHAR (25),
       SALARY DECIMAL (18, 2),
       PRIMARY KEY (ID)
    );
    

    Insert some records into the above created table using the following INSERT query −

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
    (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ),
    (2, ''Khilan'', 25, ''Delhi'', 1500.00 ),
    (3, ''Kaushik'', 23, ''Kota'', 2000.00 ),
    (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ),
    (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ),
    (6, ''Komal'', 22, ''Hyderabad'', 4500.00 ),
    (7, ''Muffy'', 24, ''Indore'', 10000.00 );
    

    Execute the below query to display all the records present in CUSTOMERS table −

    Select * from CUSTOMERS;
    

    Following is the CUSTOMERS table −

    ID NAME AGE ADDRESS SALARY
    1 Ramesh 32 Ahmedabad 2000.00
    2 Khilan 25 Delhi 1500.00
    3 Kaushik 23 Kota 2000.00
    4 Chaitali 25 Mumbai 6500.00
    5 Hardik 27 Bhopal 8500.00
    6 Komal 22 Hyderabad 4500.00
    7 Muffy 24 Indore 10000.00

    The following query selects the position of the first occurrence of a string that starts with the letter ”K” from the NAME column in the CUSTOMERS table −

    SELECT REGEXP_INSTR(NAME, ''^K'')
    AS RESULT FROM CUSTOMERS;
    

    As we can see in the output below, there are three string in NAME column that starts with letter K.

    Result
    0
    1
    1
    0
    0
    1
    0

    Client Program

    We can also perform the MySQL REGEXP_INSTR() function using the client programs (such as PHP, Node.js, Java, Python) to match specified pattern with either a string or the data in database tables.

    Syntax

    Following are the syntaxes of this operation in various programming languages −

    To retrieve all the records from a MySQL database that match a specific pattern, whether it”s a string or data, through a PHP program, we execute the ”SELECT” statement using the mysqli function query() as follows −

    $sql = "SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
    $mysqli->query($sql);
    

    To retrieve all the records from a MySQL database that match a specific pattern, whether it”s a string or data, through a Node.js program, we execute the ”SELECT” statement using the query() function of the mysql2 library as −

    sql = "SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
    con.query(sql);
    

    To retrieve all the records from a MySQL database that match a specific pattern, whether it”s a string or data, through a Java program, we execute the ”SELECT” statement using the JDBC function executeUpdate() as −

    String sql = "SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
    statement.executeQuery(sql);
    

    To retrieve all the records from a MySQL database that match a specific pattern, whether it”s a string or data, through a Python program, we execute the ”SELECT” statement using the execute() function of the MySQL Connector/Python as −

    sql = "SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''To'') AS RESULT"
    cursorObj.execute(sql)
    

    Example

    Following are the programs −

    $dbhost = ''localhost
    $dbuser = ''root
    $dbpass = ''password
    $db = ''TUTORIALS
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db);
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error); exit(); } //printf(''Connected successfully.
    ''); $sql = "SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''To'') AS RESULT"; if($result = $mysqli->query($sql)){ while($row = mysqli_fetch_array($result)){ printf("Result: %d", $row[''RESULT'']); } } if($mysqli->error){ printf("Error message: ", $mysqli->error); } $mysqli->close();

    Output

    The output obtained is as follows −

    Result: 9
    
    var mysql = require(''mysql2'');
    var con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"password"
    });
     //Connecting to MySQL
     con.connect(function(err) {
     if (err) throw err;
      //console.log("Connected successfully...!");
      //console.log("--------------------------");
     sql = "USE TUTORIALS";
     con.query(sql);
     sql = "SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
     console.log("Select query executed successfully..!");
     console.log("Table records: ");
     con.query(sql);
     con.query(sql, function(err, result){
     if (err) throw err;
     console.log(result);
     });
    });
    

    Output

    The output produced is as follows −

    Select query executed successfully..!
    Table records:
    [ { RESULT: 9 } ]
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class regexp_instr {
        public static void main(String[] args) {
            String url = "jdbc:mysql://localhost:3306/TUTORIALS";
            String user = "root";
            String password = "password";
            ResultSet rs;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user, password);
                Statement st = con.createStatement();
                //System.out.println("Database connected successfully...!");
                String sql = "SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
                rs = st.executeQuery(sql);
                      while(rs.next()) {
                    String result = rs.getString("RESULT");
                    System.out.println("Result: " + result);
                }
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Output

    The output obtained is as shown below −

    Result: 9
    
    import mysql.connector
    # Establishing the connection
    connection = mysql.connector.connect(
        host=''localhost'',
        user=''root'',
        password=''password'',
        database=''tut''
    )
    # Creating a cursor object
    cursorObj = connection.cursor()
    regexp_instr_query = f"SELECT REGEXP_INSTR(''Welcome To Tutorialspoint!'', ''To'') AS RESULT"
    cursorObj.execute(regexp_instr_query)
    # Fetching all the results
    results = cursorObj.fetchall()
    # Display the result
    print("Result of REGEXP_INSTR() Function:")
    for row in results:
        position = row[0]
        if position > 0:
            print(f"The pattern ''To'' found at position {position}")
        else:
            print("The pattern ''To'' not found in the given string")
    cursorObj.close()
    connection.close()
    

    Output

    Following is the output of the above code −

    Result of REGEXP_INSTR() Function:
    The pattern ''To'' found at position 9
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MySQL – regexp_like() Function nhận dự án làm có lương

    MySQL – REGEXP_LIKE() Function

    Table of content


    MySQL supports various types of pattern matching operations to retrieve filtered result-sets from huge database tables. But, pattern matching with regular expressions is a powerful way to perform a complex search.

    As we have seen in the previous chapter, the MySQL regexp_instr() function is used to return the position of the pattern found. But if you want to just detect whether the pattern is present in the data or not, you can use the regexp_like() function.

    MySQL REGEXP_LIKE() Function

    The MySQL regexp_like() function is also used to search for a string that is matched with specified patterns. This function returns 1 if this string matches the specified pattern, 0 if there is no match, or NULL if the string or the pattern is NULL. The pattern used in this function can be an extended regular expression and not just an ordinary string.

    Syntax

    Following is the syntax of the MySQL regexp_like() function −

    REGEXP_LIKE(expr, pattern[, match_type])
    

    Parameters

    The regexp_like() function takes following parameter values −

    • expr: The string in which search is performed

    • pattern: The pattern that is searched in the string

    • match_type: (Optional argument) A string that specifies how to perform matching; includes case-sensitive matching(c), case-insensitive matching(i), multiple-line mode(m), matching line terminators(n), matching Unix-only line endings(u).

    Example

    In this example, we are performing a search operation on a simple string using the MySQL REGEXP_LIKE() function −

    SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''To'')
    AS RESULT;
    

    The search pattern ”To” is present in the string, so it returned 1 as output.

    Result
    1

    Now, if there is no match found in the string, the result will be obtained as ”0” as shown below −

    SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''Hello'')
    AS RESULT;
    

    Following is the output −

    Result
    0

    Let us also pass the optional arguments to this function as case-sensitive matching(c) and observe the result −

    SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''t'', ''c'')
    AS RESULT;
    

    Executing the query above will produce the following output −

    Result
    1

    Example

    If either of the first two arguments passed to this function is NULL, this function returns NULL. In the below query, we are passing NULL to the string parameter.

    SELECT REGEXP_LIKE(NULL, ''value'') AS Result;
    

    Following is the output −

    Result
    NULL

    Here, we are passing NULL as the search pattern −

    SELECT REGEXP_LIKE(''Welcome to Tutorialspoint'', NULL)
    AS Result;
    

    Executing the query above will produce the following output −

    Result
    NULL

    Example

    In another example, let us perform a search operation on a database table named CUSTOMERS using the REGEXP_LIKE() function. Firstly, let us create the table using the following query −

    CREATE TABLE CUSTOMERS (
       ID INT AUTO_INCREMENT,
       NAME VARCHAR(20) NOT NULL,
       AGE INT NOT NULL,
       ADDRESS CHAR (25),
       SALARY DECIMAL (18, 2),
       PRIMARY KEY (ID)
    );
    

    The following query inserts 7 records into the above created table −

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES
    (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ),
    (2, ''Khilan'', 25, ''Delhi'', 1500.00 ),
    (3, ''Kaushik'', 23, ''Kota'', 2000.00 ),
    (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ),
    (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ),
    (6, ''Komal'', 22, ''Hyderabad'', 4500.00 ),
    (7, ''Muffy'', 24, ''Indore'', 10000.00 );
    

    Execute the following query to display all the records of CUSTOMERS table −

    Select * from CUSTOMERS;
    

    Following is the CUSTOMERS table −

    ID NAME AGE ADDRESS SALARY
    1 Ramesh 32 Ahmedabad 2000.00
    2 Khilan 25 Delhi 1500.00
    3 Kaushik 23 Kota 2000.00
    4 Chaitali 25 Mumbai 6500.00
    5 Hardik 27 Bhopal 8500.00
    6 Komal 22 Hyderabad 4500.00
    7 Muffy 24 Indore 10000.00

    The following query selects records from the CUSTOMERS table where the NAME column starts with the letter ”K” −

    SELECT REGEXP_LIKE(NAME, ''^K'')
    AS RESULT FROM CUSTOMERS;
    

    If there is a name that starts with letter ”K” it gives 1 as output, else 0 −

    Result
    0
    1
    1
    0
    0
    1
    0

    The following query checks whether the ”ADDRESS” column in the ”CUSTOMERS” table contains the letter ”K” (case-insensitive). If the address contains ”K” or ”k,” the result is 1; otherwise, it”s 0.

    SELECT REGEXP_LIKE(ADDRESS, ''R'', ''i'')
    AS RESULT FROM CUSTOMERS;
    

    As we can see in the output table, 6th and 7th row in ADDRESS column contains a letter ”K” (case-insensitive) −

    Result
    0
    1
    1
    0
    0
    1
    0

    REGEXP_LIKE() Function Using a Client Program

    Besides using MySQL queries to perform the REGEXP_Like() function, we can also use client programs such as PHP, Node.js, Java, and Python to achieve the same result.

    Syntax

    Following are the syntaxes of this operation in various programming languages −

    To search for a string that is matched with specified pattern through PHP program, we need to execute the “SELECT” statement using the mysqli function query() as follows −

    $sql = "SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
    $mysqli->query($sql);
    

    To search for a string that is matched with specified pattern through Node.js program, we need to execute the “SELECT” statement using the query() function of the mysql2 library as follows −

    sql = "SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
    con.query(sql);
    

    To search for a string that is matched with specified pattern through Java program, we need to execute the “SELECT” statement using the JDBC function executeUpdate() as follows −

    String sql = "SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
    statement.executeQuery(sql);
    

    To search for a string that is matched with specified pattern through Python program, we need to execute the “SELECT” statement using the execute() function of the MySQL Connector/Python as follows −

    sql = "SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''To'') AS RESULT"
    cursorObj.execute(sql)
    

    Example

    Following are the programs −

    $dbhost = ''localhost
    $dbuser = ''root
    $dbpass = ''password
    $db = ''TUTORIALS
    $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db);
    if ($mysqli->connect_errno) {
        printf("Connect failed: %s
    ", $mysqli->connect_error); exit(); } //printf(''Connected successfully.
    ''); $sql = "SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''To'') AS RESULT"; if($result = $mysqli->query($sql)){ while($row = mysqli_fetch_array($result)){ printf("Result: %d", $row[''RESULT'']); } } if($mysqli->error){ printf("Error message: ", $mysqli->error); } $mysqli->close();

    Output

    The output obtained is as shown below −

    Result: 1
    
    var mysql = require(''mysql2'');
    var con = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"password"
    });
     //Connecting to MySQL
     con.connect(function(err) {
     if (err) throw err;
      //console.log("Connected successfully...!");
      //console.log("--------------------------");
     sql = "USE TUTORIALS";
     con.query(sql);
     sql = "SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
     console.log("Select query executed successfully..!");
     console.log("Table records: ");
     con.query(sql);
     con.query(sql, function(err, result){
     if (err) throw err;
     console.log(result);
     });
    });
    

    Output

    The output obtained is as shown below −

    Select query executed successfully..!
    Table records:
    [ { RESULT: 1 } ]
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class regexp_like {
        public static void main(String[] args) {
            String url = "jdbc:mysql://localhost:3306/TUTORIALS";
            String user = "root";
            String password = "password";
            ResultSet rs;
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user, password);
                Statement st = con.createStatement();
                //System.out.println("Database connected successfully...!");
                String sql = "SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''To'') AS RESULT";
                rs = st.executeQuery(sql);
                while(rs.next()) {
                    String result = rs.getString("RESULT");
                    System.out.println("Result: " + result);
                }
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    Output

    The output obtained is as shown below −

    Result: 1
    
    import mysql.connector
    # Establishing the connection
    connection = mysql.connector.connect(
        host=''localhost'',
        user=''root'',
        password=''password'',
        database=''tut''
    )
    # Creating a cursor object
    cursorObj = connection.cursor()
    regexp_like_query = f"SELECT REGEXP_LIKE(''Welcome To Tutorialspoint!'', ''To'') AS RESULT"
    cursorObj.execute(regexp_like_query)
    # Fetching all the results
    results = cursorObj.fetchall()
    # Display the result
    print("Result of REGEXP_LIKE() Function:")
    for row in results:
        result = row[0]
        if result:
            print("The pattern ''To'' is found in the given string.")
        else:
            print("The pattern ''To'' is not found in the given string.")
    cursorObj.close()
    connection.close()
    

    Output

    The output obtained is as shown below −

    Result of REGEXP_LIKE() Function:
    The pattern ''To'' is found in the given string.
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc