Mysql LEFT()
MySQL LEFT() returns a specified number of characters from the left of the string. Both the number and the string are supplied as arguments of the function.
Syntax:
LEFT (string, length)
Arguments
Name | Description |
---|---|
string | The string from which a number of characters from the left are to be returned. |
length | An integer which indicates the number of characters to be returned starting from the left of the string in the first argument. |
MySQL Version: 5.6
Pictorial Presentation
Example of MySQL LEFT() function
The following MySQL statement returns the pub_name and 5 characters from the left of pub_name from the publisher table.
Code:
SELECT pub_name, LEFT(pub_name, 5)
FROM publisher;
Sample table: publisher
Sample Output:
mysql> SELECT pub_name, LEFT(pub_name, 5) -> FROM publisher; +------------------------------+-------------------+ | pub_name | LEFT(pub_name, 5) | +------------------------------+-------------------+ | Jex Max Publication | Jex M | | BPP Publication | BPP P | | New Harrold Publication | New H | | Ultra Press Inc. | Ultra | | Mountain Publication | Mount | | Summer Night Publication | Summe | | Pieterson Grp. of Publishers | Piete | | Novel Publisher Ltd. | Novel | +------------------------------+-------------------+ 8 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>example-left-function - php mysql examples | w3resource</title>
<meta name="description" content="example-left-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 name and leftmost 5 characters of that name:</h2>
<table class='table table-bordered'>
<tr>
<th>Publishers name</th><th>leftmost 5 characters of Publishers name</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,LEFT(pub_name,5)
FROM publisher') as $row) {
echo "<tr>";
echo "<td>" . $row['pub_name'] . "</td>";
echo "<td>" . $row['LEFT(pub_name,5)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
JSP script
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-left-function</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT pub_name,LEFT(pub_name,5) FROM publisher";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Publishers name</td>
<td>leftmost 5 characters of Publishers name</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("pub_name")%></TD>
<TD><%=rs.getString("LEFT(pub_name,5)")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Cant connect to database.");
}
%>
</body>
</html>
Example of MySQL LEFT() function with where clause
The following MySQL statement returns the pub_name(s) who belong to the USA and 5 characters from the left of those pub_name(s) from the publisher table.
Code:
SELECT pub_name,LEFT(pub_name,5)
FROM publisher
WHERE country='USA';
Sample table: publisher
Sample Output:
mysql> SELECT pub_name,LEFT(pub_name,5) -> FROM publisher -> WHERE country='USA'; +--------------------------+------------------+ | pub_name | LEFT(pub_name,5) | +--------------------------+------------------+ | Jex Max Publication | Jex M | | Mountain Publication | Mount | | Summer Night Publication | Summe | +--------------------------+------------------+ 3 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-left-function - php mysql examples | w3resource</title> <meta name="description" content="example1-left-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 those who belong to USA and leftmost 5 characters of their name:</h2> <table class='table table-bordered'> <tr> <th>Publishers name</th><th>leftmost 5 characters of Publishers name</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,LEFT(pub_name,5) FROM publisher WHERE country="USA"') as $row) { echo "<tr>"; echo "<td>" . $row['pub_name'] . "</td>"; echo "<td>" . $row['LEFT(pub_name,5)'] . "</td>"; echo "</tr>"; } ?> </tbody></table> ></div> </div> </div> </body> </html>
0 comments:
Post a Comment