SQL FIND_IN_SET 函数
一、SQL FIND_IN_SET 函数 语法
SQL语言的 FIND_IN_SET 函数显示搜索到的字符串在多个字符串中的位置。
语法1:此语法使用带有 SQL 表的两个或多个列名的 FIND_IN_SET 函数:
SELECT FIND_IN_SET(Searched_String, Column_Name1, column_Name2, Column_Name3,……. Column_NameN) AS Alias_Name FROM Table_Name;
在 Find_In_Set 语法中,我们必须指定要在其中搜索字符串的那些列的名称。
语法2:此语法使用带有字符串的 FIND_IN_SET 函数:
SELECT FIND_IN_SET(Searched_string, "String1, String2, String3, …… StringN") AS Alias_Name;
语法3:此语法使用带有单个字符的 FIND_IN_SET 函数:
SELECT FIND_IN_SET(Searched_character, "Character_1, character_2, Character_3, ……., Character_N") AS Alias_Name;
二、SQL FIND_IN_SET 函数 示例
示例 1:以下查询显示了“S”字符在给定字符列表中的位置:
SELECT FIND_IN_SET( 'S', ''H, I, A, P, Q, S, R, T, V, M'' ) AS S_Findinset;
输出结果为:
S_Findinset |
6 |
示例 2:以下语句显示了“u”字符在 Congratulations 字符串中的位置:
SELECT FIND_IN_SET( 'u', ''C, o, n, g, r, a, t, u, l, a, t, i, o, n, s '') AS Wishes u_Findinset;
输出结果为:
S_Findinset |
8 |
示例 3:以下 SELECT 查询在字符串列表中查找“NEW”字符串的位置:
SELECT FIND_IN_SET('NEW', ''NEW, DELHI, IS, THE, CAPITAL, OF, INDIA'') AS NEW_Findinset;
输出结果为:
NEW_Findinset |
1 |
示例 4:此示例对结构化查询语言中的表使用 FIND_IN_SET 函数。
在这个例子中,我们必须创建一个新的 SQL 表,通过它我们将对列执行 FIND_IN_SET() 函数。
在 SQL 数据库中创建新表的语法如下:
CREATE TABLE table_name
(
First_Column_of_table Data Type (character_size of First Column),
Second_Column_of_table Data Type (character_size of the Second column ),
Third_Column_of_table 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表中插入学生的成绩和分数记录:
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 |
查询 1:以下 SELECT 查询对上述 Student_Grade 表的 First_City、Second_City、New_City 列使用 FIND_IN_SET 函数:
SELECT First_City, Second_City, New_City FIND_IN_SET('Ghaziabad', First_City, Second_City, New_City) AS Ghaziabad_Findinset FROM Student_Grade;
此 SQL 语句显示了Ghaziabad市在表的给定三列中的位置。
输出结果为:
First_City | Second_City | New_City | Ghaziabad_Findinset |
---|---|---|---|
Lucknow | Chandigarh | Ghaziabad | 3 |
Chandigarh | Ghaziabad | Ghaziabad | 3 |
Delhi | Ghaziabad | Lucknow | 2 |
Ghaziabad | Delhi | Lucknow | 1 |
Delhi | Kanpur | Ghaziabad | 3 |
Ghaziabad | Meerut | Chandigarh | 1 |
Lucknow | Ghaziabad | Chandigarh | 2 |
查询 2:以下 SELECT 查询将 FIND_IN_SET 函数与上述 Student_Grade 表中 Roll_No 大于 2 的学生的 Hindi_Marks 和 Maths_Marks 列一起使用:
SELECT Roll_No, Hindi_Marks, Maths_Marks(95, Hindi_Marks, Maths_Marks) AS 95_Findinset FROM Student_Grade WHERE Roll_No > 2;
输出结果为:
Roll_No | Hindi_Marks | Maths_Marks | 95_Findinset |
---|---|---|---|
07 | 91 | 95 | 2 |
04 | 85 | 82 | Null |
11 | 95 | 97 | 1 |
16 | 95 | 90 | 1 |
19 | 89 | 95 | 2 |
热门文章
优秀文章