SQL LIKE函数

SQL LIKE函数

Like(String)是结构化查询语言中的一个函数,它用于将模式与字符串进行匹配。如果字符串与模式不匹配,该函数返回0,否则返回1。我们也可以将Like函数与表的列一起使用。

LIKE字符串函数的语法

语法1: 这个语法使用了SQL表的两个列与LIKE函数一起使用:

SELECT Column_Name LIKE Pattern FROM Table_Name;

在语法中,我们需要定义要与给定模式匹配的那一列的名称。

语法3: 这个语法使用了LIKE函数和两个字符串:

SELECT String LIKE Pattern;

LIKE字符串函数的示例

示例1: 以下SELECT查询比较了JAVATPOINT和EXAMPLES字符串的长度:

SELECT 'JAVATPOINT' LIKE 'EXAMPLES';

输出:

0

示例 2: 下面的SELECT查询将字符串’Javatpoint is good’与给定的模式匹配:

SELECT ' javatpoint is good' LIKE 'javatpoint is good';

输出:

1

示例3: 以下SELECT查询比较了两个城市的长度:

SELECT 'Delhi' LIKE 'Noida';

输出:

0

例4:此示例使用LIKE函数与SQL表

在此示例中,我们将创建一个新的SQL表,在该表上必须执行LIKE函数以将列值与模式进行匹配。

在SQL数据库中创建新表的语法如下:

CREATE TABLE table_name
(
First_Column_of_SQLtable Data Type (character_size of First Column),  
Second_Column_of_SQLtable Data Type (character_size of the Second column ),  
Third_Column_of_SQLtable Data Type (character_size of the Third column),  
...  

Last_Column_of_table Data Type (character_size of the Last column)
);  

下面的CREATE语句创建了 Student_Grade 表:

CREATE TABLE Student_Grade
(
Roll_No INT PRIMARY KEY,  
First_Name VARCHAR (100),  
Last_Name VARCHAR (100), 
First_City Varchar(120),
Second_City Varchar(120),
New_City Varchar(120),
Hindi_Marks INT, 
Maths_Marks INT, 
Grade Varchar (80)
);

下面的INSERT查询将学生的成绩和分数插入到 Student_Grade 表中的recLIKEs:

INSERT INTO Student_Grade (Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) VALUES (10, Aman, Sharma, Lucknow Chandigarh, Ghaziabad, 88, 95, A2);

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES ( 02, Vishal, Sharma, Chandigarh, Ghaziabad, Delhi, 95, 82, A1 );

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES (07, Raj, Gupta, Delhi, Ghaziabad, Lucknow, 91, 95, A1);

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade)  
VALUES (04, Yash, Singhania, Ghaziabad, Delhi, Lucknow, 85, 82, A2);

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES (11, Vinay, Roy, Delhi, Kanpur, Ghaziabad, 95, 97, A1);

INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES (16, Manoj, Gupta, Ghaziabad, Meerut, Chandigarh, 95, 90, B1);


INSERT INTO Student_Grade 
(Roll_No, First_Name, Last_Name, First_City, Second_City, New_City, Hindi_Marks, Maths_Marks, Grade) 
VALUES (19, Ram, Gupta, Lucknow, Ghaziabad, Chandigarh, 89, 95, A2);

下面的SELECT语句显示了上面 Student_Grade 表中插入的记录:

SELECT * FROM Student_Grade; 
Roll_No First_Name Last_Name First_City Second_City New_City Hindi_Marks Maths_Marks Grade
10 Aman Sharma Lucknow Chandigarh Ghaziabad 88 95 A2
02 Vishal Sharma Chandigarh Ghaziabad Ghaziabad 95 82 A1
07 Raj Gupta Delhi Ghaziabad Lucknow 91 95 A1
04 Yash Singhania Ghaziabad Delhi Lucknow 85 82 A2
11 Vinay Roy Delhi Kanpur Ghaziabad 95 97 A1
16 Manoj Gupta Ghaziabad Meerut Chandigarh 95 90 B1
19 Ram Gupta Lucknow Ghaziabad Chandigarh 89 95 A2

下面的SELECT查询将上表的Worker_First_Name列与指定的模式进行匹配:

SELECT Worker_First_Name, Worker_First_Name LIKE Avinash AS Compare_Firstname_City FROM Worker_Info;

输出:

Worker_First_Name Compare_Firstname_City
Arush 0
Bulbul 0
Saurabh 0
Shivani 0
Avinash 1
Shyam 0

以下SELECT查询使用了以上Worker_Info表中的Worker_first_Name和Worker_Last_Name列的LIKE()函数:

SELECT Worker_First_Name, Worker_Last_Name, Worker_First_Name LIKE Worker_Last_Name AS LIKE_Firstname_Lastname FROM Worker_Info;

这个SQL语句匹配了姓和名两列的姓名。

输出:

Worker_First_Name Worker_Last_Name LIKE_Firstname_Lastname
Arush Sharma 0
Bulbul Roy 0
Saurabh Roy 0
Shivani Singhania 0
Avinash Sharma 0
Shyam Besas 0

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程