SQL FIELD函数

SQL FIELD函数

在结构化查询语言中,FIELD字符串函数返回给定字符串在字符串列表中的位置。如果给定字符串在字符串列表中找不到,则FIELD函数返回结果为0。

FIELD字符串函数的语法

在SQL中,我们可以将FIELD函数与表的列、字符串和字符一起使用。

语法1:

SELECT FIELD(Searched_value, Column_Name1, column_Name2, Column_Name3,……. Column_NameN) AS Alias_Name FROM Table_Name;

在这个语法中,我们使用了SQL的现有表中的FIELD函数。在这里,我们必须定义我们想要在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函数和字符列表。

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: 下面的查询搜索不在字符串列表中的”Old”字符串:

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 查询使用了上述 Fresher_Marks 表的 First_Name、Middle_Name、Last_Name 列上的 FIELD 函数:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程