Tuesday 10 July 2018

Mysql LOCATE() function

Mysql LOCATE() function

MySQL LOCATE() returns the position of the first occurrence of a string within a string. Both of these strings are passed as arguments. An optional argument may be used to specify from which position of the string (i.e. string to be searched) searching will start. If this position is not mentioned, searching starts from the beginning.
Syntax:
LOCATE(substr,str)
LOCATE (search str, str, [position])
Arguments
NameDescription
search strA string which will be searched for.
strA string which is going to be searched.
positionPosition from where (within the second argument) the searching will start .
MySQL Version: 5.6
Pictorial Presentation
MySQL LOCATE function
Example: MySQL LOCATE() function
The following MySQL statement returns the 1st occurrence ‘st’ within the string ‘myteststring’. Since the "st" subsrting is found at the fifth position, the function returns 5.
Code:
SELECT LOCATE('st','myteststring'); 


Sample Output:
mysql> SELECT LOCATE('st','myteststring'); 
+-----------------------------+
| LOCATE('st','myteststring') |
+-----------------------------+
|                           5 | 
+-----------------------------+
1 row in set (0.02 sec)
Example: MySQL LOCATE() function with starting position
The following statement returns the 1st occurrence ‘st’ within the string ‘myteststring’ and the searching will start from the 6th position of the string. The function returns 7, since the substring st is found at seventh position.
Code:
SELECT LOCATE('st','myteststring',6);


Sample Output:
mysql> SELECT LOCATE('st','myteststring',6);
+-------------------------------+
| LOCATE('st','myteststring',6) |
+-------------------------------+
|                             7 | 
+-------------------------------+
1 row in set (0.00 sec)
Example: MySQL LOCATE() function using table
The following MySQL statement returns those rows from the publisher table where the search string ‘at’ exists at least once within the column pub_name.
Code:
SELECT pub_name,LOCATE('at',pub_name) 
FROM publisher 
WHERE locate('at',pub_name)>0; 


Sample table: publisher

Sample Output:
mysql> SELECT pub_name,LOCATE('at',pub_name) 
    -> FROM publisher 
    -> WHERE locate('at',pub_name)>0;
+--------------------------+-----------------------+
| pub_name                 | LOCATE('at',pub_name) |
+--------------------------+-----------------------+
| Jex Max Publication      |                    15 | 
| BPP Publication          |                    11 | 
| New Harrold Publication  |                    19 | 
| Mountain Publication     |                    16 | 
| Summer Night Publication |                    20 | 
+--------------------------+-----------------------+
5 rows in set (0.03 sec)

Example: MySQL LOCATE() function with WHERE clause
The following MySQL statement returns those rows from the publisher table where the search string ‘at’ present at least once within the column pub_name. In this statement the 1st locate starts the searching from the beginning of the string and the second searching starts from the 16th position of the string.
Code:
SELECT pub_name,LOCATE('at',pub_name),
LOCATE('at',pub_name,16)
FROM publisher 
WHERE LOCATE('at',pub_name)>0;


Sample table: publisher

Sample Output:
mysql> SELECT pub_name,LOCATE('at',pub_name),
    -> LOCATE('at',pub_name,16)
    -> FROM publisher 
    -> WHERE LOCATE('at',pub_name)>0;
+--------------------------+-----------------------+--------------------------+
| pub_name                 | LOCATE('at',pub_name) | LOCATE('at',pub_name,16) |
+--------------------------+-----------------------+--------------------------+
| Jex Max Publication      |                    15 |                        0 | 
| BPP Publication          |                    11 |                        0 | 
| New Harrold Publication  |                    19 |                       19 | 
| Mountain Publication     |                    16 |                       16 | 
| Summer Night Publication |                    20 |                       20 | 
+--------------------------+-----------------------+--------------------------+
5 rows in set (0.00 sec)
PHP script
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-locate-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-locate-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>List of publishers, whose name contains the string 'at' at least once where string is positioned as sixteenth character from the starting:</h2>
<table class='table table-bordered'>
<tr>
<th>Publishers name</th><th>Output</th><th>Output2</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT pub_name,LOCATE("at",pub_name) as output, 
LOCATE("at",pub_name,16) as output2 
FROM publisher
WHERE  LOCATE("at",pub_name)>0') as $row) {
echo "<tr>";
echo "<td>" . $row['pub_name'] . "</td>";
echo "<td>" . $row['output'] . "</td>";
echo "<td>" . $row['output2'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

0 comments:

Post a Comment