Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:51:22 (UTC/GMT +8 giờ)Hàm trái ()
Mysql trái () trả về một số ký tự được chỉ định từ bên trái của chuỗi. Cả số và chuỗi được cung cấp dưới dạng đối số của hàm.
Syntax:
LEFT (string, length)
Tranh luận
Tên | Sự mô tả |
---|
sợi dây | Chuỗi mà một số ký tự từ bên trái sẽ được trả về. |
chiều dài | Một số nguyên biểu thị số lượng ký tự được trả về bắt đầu từ bên trái của chuỗi trong đối số đầu tiên. |
Sơ đồ cú pháp:
Phiên bản MySQL: 5.6
Trình chiếu video:
Trình duyệt của bạn không hỗ trợ video HTML5.
Trình bày bằng hình ảnh:
Ví dụ về hàm mysql trái ()
Câu lệnh MySQL sau đây trả về pub_name và 5 ký tự từ bên trái của pub_name từ bảng nhà xuất bản.
Code:
SELECT pub_name, LEFT(pub_name, 5)
FROM publisher;
Bảng mẫu: Nhà xuất bản
Đầu ra mẫu:
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)
Kịch bản PHP:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<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>
Xem ví dụ trong trình duyệt
Script jsp:
<%@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>
Ví dụ về hàm mysql trái () với mệnh đề where
Câu lệnh MySQL sau đây trả về pub_name (s) thuộc về Hoa Kỳ và 5 ký tự từ bên trái của các pub_name đó từ bảng nhà xuất bản.
Code:
SELECT pub_name,LEFT(pub_name,5)
FROM publisher
WHERE country='USA';
Bảng mẫu: Nhà xuất bản
Đầu ra mẫu:
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)
Kịch bản PHP:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<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>
Xem ví dụ trong trình duyệt
Script jsp:
Ví dụ về hàm mysql trái () với mệnh đề where
Câu lệnh MySQL sau đây trả về pub_name (s) thuộc về Hoa Kỳ và 5 ký tự từ bên trái của các pub_name đó từ bảng nhà xuất bản. LCASE
Next:
LENGTH
1 câu trả lời..
Bạn có thể sử dụng hàm MySQL Subring () theo cách này:.
Hãy xem video này để hiểu nơi sử dụng các chức năng chuỗi với cú pháp và ví dụ ..
Các tham số được sử dụng trong phương thức phụ như sau:.
Col_name: Điều này được yêu cầu để trích xuất chuỗi ..
1: Điều này là bắt buộc cho vị trí bắt đầu ..
SQL Server fort () hàm bên trái () hàm trích xuất một số ký tự từ một chuỗi (bắt đầu từ trái).LEFT() Function
The LEFT() function extracts a number of characters from a string (starting from left).