SQL MAKE_SET函数
结构化查询语言中的MAKE_SET字符串函数返回给定位于多个值集合中的位的值。
MAKE_SET字符串函数的语法
在SQL中,我们可以将MAKE_SET函数与表的列、字符串和字符一起使用。
语法1:
SELECT MAKE_SET(bits, Column_Name1, column_Name2, Column_Name3,……. Column_NameN) AS Alias_Name FROM Table_Name;
在这个语法中,我们使用了SQL中现有表的MAKE_SET函数。在这里,我们必须定义我们想要对其执行MAKE_SET函数的表的名称和列。
语法 2:
SELECT MAKE_SET(bits, "String1", "String2", "String3", …… "StringN") AS Alias_Name;
在这个语法中,我们使用了一个字符串列表来调用MAKE_SET函数。
语法3:
SELECT MAKE_SET(bits, "Character_1", "character_2", "Character_3", ……., "Character_N") AS Alias_Name;
在这个语法中,我们使用了带有字符列表的MAKE_SET函数。
MAKE_SET字符串函数的示例
示例1: 下面的查询是使用带有字符串列表的MAKE_SET函数:
SELECT MAKE_SET( 1, ''H'', ''I'', ''A'', ''P'', ''Q'', ''S'', ''R'', ''T'', ''V'', ''M'' ) AS Value_at1st_bit;
输出:
Value_at1st_bit
---
H
示例2: 以下查询搜索1位和4位的字符串:
SELECT MAKE_SET(1 | 4, ''New'', ''Delhi'', ''is'', ''the'', ''Capital'', ''of'', ''India'') AS Value_at1st_4th_bit;
输出:
Value_at1st_4th_bit
---
New, is
示例3: 给定集合中第二个位的查询结果如下:
SELECT MAKE_SET( 2, ''H'', ''I'', ''V'', ''M'' ) AS Value_at2nd_bit;
输出:
Value_at2nd_bit
---
I
示例 4: 下面的查询显示给定集合中的第二位和第四位的值:
SELECT MAKE_SET( 2 | 4, ''H'', ''I'', ''A'', ''P'', ''Q'', ''S'', ''R'', ''T'', ''V'', ''M'' ) AS Value_at2_4_bit;
输出:
Value_at2_4_bit
---
I, A
示例 5: 此示例使用结构化查询语言中的MAKE_SET函数与表。
在这个第四个示例中,我们将通过一个新的表来使用MAKE_SET函数与表的值:
以下代码块显示了在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 列,并使用了 MAKE_SET 函数:
SELECT First_Name, Middle_Name, Last_Name, MAKE_SET(2, First_Name, Middle_Name, Last_Name) AS Value_at_2bit FROM Fresher_Marks;
这个查询显示了上表中每个新生的Middle name的值。
输出结果:
First_Name | Middle_Name | Last_Name | Value_at_2bit |
---|---|---|---|
Vinay | Roy | Gupta | Roy |
Monu | Roy | Singhania | Roy |
Ravi | Roy | Kumar | Roy |
Shyam | Roy | Sharma | Roy |
Abhay | Kumar | Gupta | Kumar |
Riya | Roy | Sharma | Roy |
Vishal | Kumar | Sharma | Kumar |
查询2: 下面的SELECT查询使用MAKE_SET函数,使用上述新生分数表中Fresher_ID大于502的学生的Aptitude_Marks、Reasoning_Marks和Technical_Marks列:
SELECT Fresher_ID, Aptitude_Marks, Reasoning_Marks, Technical_Marks, MAKE_SET(4, Aptitude_Marks, Reasoning_Marks, Technical_Marks) AS Value_at_4bit FROM Fresher_Marks WHERE Fresher_ID > 502;
输出:
Fresher_ID | Aptitude_Marks | Reasoning_Marks | Technical_Marks | Value_at_4bit |
---|---|---|---|---|
504 | 71 | 82 | 69 | 69 |
507 | 85 | 90 | 68 | 68 |
510 | 45 | 68 | 82 | 82 |
509 | 68 | 90 | 69 | 69 |
505 | 75 | 65 | 88 | 88 |
查询3: 以下SELECT查询使用了上述Fresher_Marks表的First_Name,Middle_Name,Last_Name列的MAKE_SET函数:
SELECT First_Name, Middle_Name, Last_Name, MAKE_SET(1 | 3, First_Name, Middle_Name, Last_Name) AS Value_at_1_3bit FROM Fresher_Marks;
此查询显示了上表中每个刚出校园的人的Middle name的值。
输出:
First_Name | Middle_Name | Last_Name | Value_at_1_3bit |
---|---|---|---|
Vinay | Roy | Gupta | Vinay, Roy |
Monu | Roy | Singhania | Monu, Roy |
Ravi | Roy | Kumar | Ravi, Roy |
Shyam | Roy | Sharma | Shyam, Roy |
Abhay | Kumar | Gupta | Abhay, Kumar |
Riya | Roy | Sharma | Riya, Roy |
Vishal | Kumar | Sharma | Vishal, Kumar |