SQL FIELD 函数
一、SQL FIELD 函数 语法
SQL语言中的 FIELD 字符串函数从字符串列表中返回给定字符串的位置。如果在字符串列表中找不到给定的字符串,则 FIELD 函数在结果中返回 0。
在 SQL 中,我们可以对表的列、字符串和字符使用 FIELD 函数。
语法 1:
SELECT FIELD(Searched_value, Column_Name1, column_Name2, Column_Name3,……. Column_NameN) AS Alias_Name FROM Table_Name;
在此语法中,我们将 FIELD 函数与现有的 SQL 表一起使用。在这里,我们必须定义要在其上执行 FIELD 功能的表的名称和列。
语法 2:
SELECT FIELD("Searched_String", "String1", "String2", "String3", …… "StringN") AS Alias_Name;
在此语法中,我们将 FIELD 函数与字符串列表一起使用。
语法 3:
SELECT FIELD("Searched_character", "Character_1", "character_2", "Character_3", ……., "Character_N") AS Alias_Name;
在此语法中,我们将 FIELD 函数与字符列表一起使用。
二、SQL FIELD 函数 示例
示例 1:以下查询将 FIELD 函数与字符串列表一起使用:
SELECT FIELD( ''S'', ''H'', ''I'', ''A'', ''P'', ''Q'', ''S'', ''R'', ''T'', ''V'', ''M'' ) AS Position_of_S;
输出结果为:
Position_of_S |
---|
6 |
示例 2:以下查询搜索字符串列表中不存在的“旧”字符串:
SELECT FIELD(''Old'', ''New'', ''Delhi'', ''is'', ''the'', ''Capital'', ''of'', ''India'') AS Position_of_Old;
输出结果为:
Position_of_Old |
---|
0 |
示例 3:以下 SELECT 查询将 FIELD 函数与数字列表一起使用:
SELECT FIELD(8, 0, 8, 5, 2, 9, 7, 9, 10, 12, 4, 3) AS Position_of_8;
输出结果为:
Position_of_8 |
---|
2 |
示例 4:此示例将 FIELD 函数与结构化查询语言中的表一起使用。
在第四个示例中,我们将创建新表,通过该表我们将使用表值执行 FIELD 函数:
以下块显示了在 SQL 中创建新表的语法:
CREATE TABLE Name_of_New_Table
(
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 语句创建Fresher_Marks表:
CREATE TABLE Fresher_Marks
(
Fresher_ID INT PRIMARY KEY,
First_Name VARCHAR (100),
Middle_Name Varchar(120),
Last_Name VARCHAR (200),
City Varchar(120),
Aptitude_Marks INT,
Reasoning_Marks INT,
Technical_Marks INT,
Percentage INT
);
下面的 INSERT 语句在Fresher_Marks表中插入带有标记和详细信息的新生记录:
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (501, Vinay, Roy, Gupta, Lucknow, 85, 92, 78, 85 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (502, Monu, Roy, Singhania, Chandigarh, 54, 68, 98, 88 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (504, Ravi, Roy, Kumar, Lucknow, 71, 82, 69, 71 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (507, Shyam, Roy, Sharma, Delhi, 85, 90, 68, 78 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (510, Abhay, Kumar, Gupta, Chandigarh, 45, 68, 82, 78);
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (509, Riya, Roy, Sharma, Delhi, 68, 90, 69, 91 );
INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (505, Vishal, Kumar, Sharma, Mumbai, 75, 65, 88, 75 );
以下 SELECT 语句显示上述Fresher_Marks表的插入记录:
SELECT * FROM Fresher_Marks;
输出结果为:
Fresher_ID | First_Name | Middle_Name | Last_Name | City | Aptitude_Marks | Reasoning_Marks | Technical_Marks | Percentage |
---|---|---|---|---|---|---|---|---|
501 | Vinay | Roy | Gupta | Lucknow | 85 | 92 | 78 | 85 |
502 | Monu | Roy | Singhania | Chandigarh | 54 | 68 | 98 | 88 |
504 | Ravi | Roy | Kumar | Lucknow | 71 | 82 | 69 | 71 |
507 | Shyam | Roy | Sharma | Delhi | 85 | 90 | 68 | 78 |
510 | Abhay | Kumar | Gupta | Chandigarh | 45 | 68 | 82 | 78 |
509 | Riya | Roy | Sharma | Delhi | 68 | 90 | 69 | 91 |
505 | Vishal | Kumar | Sharma | Mumbai | 75 | 65 | 88 | 75 |
查询 1:以下 SELECT 查询将 FIELD 函数与上述 Fresher_Marks 表的 First_Name、Middle_Name、Last_Name 列一起使用:
SELECT First_Name, Middle_Name, Last_Name, FIELD('Kumar', First_Name, Middle_Name, Last_Name) AS Position_of_Kumar FROM Fresher_Marks;
此 SQL 语句显示 Kumar 在名字、中间名和姓氏中的位置。
输出结果为:
First_Name | Middle_Name | Last_Name | Position_of_Kumar |
---|---|---|---|
Vinay | Roy | Gupta | 0 |
Monu | Roy | Singhania | 0 |
Ravi | Roy | Kumar | 3 |
Shyam | Roy | Sharma | 0 |
Abhay | Kumar | Gupta | 2 |
Riya | Roy | Sharma | 0 |
Vishal | Kumar | Sharma | 2 |
查询 2:以下 SELECT 查询将 FIELD 函数与上述 Fresher_Marks 表中 Fresher_ID 大于 502 的学生的 Aptitude_Marks、Reasoning_Marks 和 Technical_Marks 列一起使用:
SELECT Fresher_ID, Aptitude_Marks, Reasoning_Marks, Technical_Marks, FIELD(95, Aptitude_Marks, Reasoning_Marks, Technical_Marks) AS Position_of_68 FROM Fresher_Marks WHERE Fresher_ID > 502;
输出结果为:
Fresher_ID | Aptitude_Marks | Reasoning_Marks | Technical_Marks | Position_of_68 |
---|---|---|---|---|
504 | 71 | 82 | 69 | 0 |
507 | 85 | 90 | 68 | 3 |
510 | 45 | 68 | 82 | 2 |
509 | 68 | 90 | 69 | 1 |
505 | 75 | 65 | 88 | 0 |
热门文章
优秀文章