SQL REPEAT函数

SQL REPEAT函数

REPEAT字符串函数将字符串在输出中重复给定的次数。

REPEAT字符串函数的语法

语法1: 此语法使用SQL表的列名与REPEAT函数结合使用:

SELECT REPEAT(Column_Name, Repetation_value) AS Alias_Name FROM Table_Name;

在语法中,我们必须指定要执行REPEAT函数的列的名称。

语法2: 此语法使用带有字符串的REPEAT函数:

SELECT REPEAT(Original_String, Repetation_value) AS Alias_Name;

语法2: 此语法使用REPEAT函数和单个字符:

SELECT REPEAT(Character, Repetation_value) AS Alias_Name;

REPEAT字符串函数示例

示例 1: 以下SELECT查询显示了结果中将JavaTpoint字符串重复四次的情况:

SELECT REPEAT(  'JAVATPOINT ', 4)AS REPEAT_4_string;

输出:

重复4字符串

JAVATPOINT JAVATPOINT JAVATPOINT JAVATPOINT

示例2: 下面的SELECT查询在输出中显示了给定的单词集两次:

SELECT REPEAT(  'NEW DELHI IS THE CAPITAL OF INDIA', 2) AS REPEAT_2_string;

输出:

REPEAT_2_string

新德里是印度的首都新德里是印度的首都

示例3: 下面的SELECT查询在结果中五次显示字符S:

SELECT REPEAT(  'S', 5) AS REPEAT_5_character;

输出:

REPEAT_5_character

SSSSS

示例 4: 下面的SELECT查询在结果中显示了六个数字的集合:

SELECT REPEAT(  '9825 ', 6) AS REPEAT_6_numbers;

输出:

REPEAT_6_numbers

9825 9825 9825 9825 9825 9825

示例5:此示例使用REPEAT函数与结构化查询语言中的表格。

要理解使用SQL的REPEAT函数,首先我们必须使用CREATE语句创建SQL表。创建新表格在SQL数据库中的语法如下:

CREATE TABLE table_name
(
First_Column_of_table Data Type (character_size of 1st Column),  
Second_Column_of_table Data Type (character_size of the 2nd column ),  
Third_Column_of_table Data Type (character_size of the 3rd column),  
...  

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

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

CREATE TABLE Student_Marks
(
Student_ID INT NOT NULL PRIMARY KEY,  
Student_First_Name VARCHAR (100),  
Student_Middle_Name VARCHAR (100),  
Student_Last_Name VARCHAR (100), 
Student_Class INT NOT NULL,
Student_City Varchar(120),
Student_State Varchar (80),
Student_Marks INT 
);

以下INSERT查询将大学院系的记录插入到 Student_Marks 表中:

INSERT INTO Student_Marks (Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) VALUES (4001, Aman, Roy, Sharma, 4, Chandigarh, Punjab, 88);

INSERT INTO Student_Marks 
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) 
VALUES ( 4002, Vishal, Gurr, Sharma, 8, Murthal, Haryana, 95 );

INSERT INTO Student_Marks 
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) 
VALUES (4007, Raj, singhania, Gupta, 6, Ghaziabad, Uttar Pradesh, 91);

INSERT INTO Student_Marks 
(Student_ID, Student_First_Name, Student_Middle_Name Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks)  
VALUES (4004, Yash, Chopra, Singhania, 9, Jaipur, Rajasthan, 85);

INSERT INTO Student_Marks 
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) 
VALUES (4011, Vinay, Sharma, Roy, 8, Chandigarh, Punjab, 94);

INSERT INTO Student_Marks 
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) 
VALUES (4006, Manoj, singhania, Gupta, 5, Ghaziabad, Uttar Pradesh, 83);


INSERT INTO Student_Marks 
(Student_ID, Student_First_Name, Student_Middle_Name, Student_Last_Name, Student_Class, Student_City, Student_State, Student_Marks) 
VALUES (4010, Ram, Raheem, Gupta, 9, Lucknow, Uttar Pradesh, 89);

以下SELECT语句显示了上述 Student_Marks 表中插入的记录:

SELECT * FROM Student_Marks;
Schoolboy_Id Schoolboy_First_Name Schoolboy_Middle_Name Schoolboy_Last_Name Schoolboy_Class Schoolboy_City Schoolboy_State Schoolboy_Marks
4001 Aman Roy Sharma 4 Chandigarh Punjab 88
4002 Vishal Gurr Sharma 8 Murthal Haryana 95
4007 Raj singhania Gupta 6 Ghaziabad Uttar Pradesh 91
4004 Yash Chopra Singhania 9 Jaipur Rajasthan 85
4011 Vinay Sharma Roy 8 Chandigarh Punjab 94
4006 Manoj Singhania Gupta 5 Ghaziabad Uttar Pradesh 83
4010 Ram Raheem Gupta 9 Lucknow Uttar Pradesh 89

查询1: 以下SELECT查询使用了上述Student_Marks表的Student_First_Name列的REPEAT函数:

SELECT Student_First_Name, REPEAT(Student_First_Name, 3) AS REPEAT_3FirstName FROM Student_Marks;

这个SQL语句在结果中显示每个学生的名字三次。

输出:

Student_First_Name REPEAT_3FirstName
Aman AmanAmanAman
Vishal VishalVishalVishal
Raj RajRajRaj
Yash YashYashYash
Vinay VinayVinayVinay
Manoj ManojManojManoj
Ram RamRamRam

查询2: 以下SELECT查询使用了上述Student_Marks表的Student_Last_Name列的REPEAT函数:

SELECT Student_Last_Name, REPEAT(Student_Last_Name, 2) AS REPEAT_2LastName FROM Student_Marks;

这个SQL语句在结果中显示了每个学生的姓氏两次。

输出:

Student_Last_Name REPEAT_2LastName
Sharma SharmaSharma
Sharma SharmaSharma
Gupta GuptaGupta
Singhania SinghaniaSinghania
Roy RoyRoy
Gupta GuptaGupta
Gupta GuptaGupta

查询3: 下面的SELECT查询使用了上述Student_Marks表中的Student_Address列的REPEAT函数:

SELECT Student_State, REPEAT(Student_State, 4) AS REPEAT_4State FROM Student_Marks;

这个 SQL 语句在结果中显示每个学生的状态四次。

输出:

学生状态 重复4次的状态
旁遮普 旁遮普旁遮普旁遮普旁遮普
哈里亚纳邦 哈里亚纳邦哈里亚纳邦哈里亚纳邦哈里亚纳邦
北方邦 北方邦北方邦北方邦北方邦
拉贾斯坦邦 拉贾斯坦邦拉贾斯坦邦拉贾斯坦邦拉贾斯坦邦
旁遮普 旁遮普旁遮普旁遮普旁遮普
北方邦 北方邦北方邦北方邦北方邦
北方邦 北方邦北方邦北方邦北方邦

查询4: 以下SELECT查询在上面的Student_Marks表格中使用了REPEAT函数,使用了Student_Middle_Name和Student_City列:

SELECT Student_Middle_Name, REPEAT(Student_Middle_Name, 2) AS REPEAT_2Middlename, Student_City, REPEAT(Student_City, 3) AS REPEAT_3City FROM Student_Marks;

这个SQL语句在结果中将中间名两次和城市三次显示给每个学生。

输出:

Student_middle_Name REPEAT_2Middlename Student_City REPEAT_3City
Roy RoyRoy Chandigarh ChandigarhChandigarhChandigarh
Gurr GurrGurr Murthal MurthalMurthalMurthal
singhania singhaniasinghania Ghaziabad GhaziabadGhaziabadGhaziabad
Chopra ChopraChopra Jaipur JaipurJaipurJaipur
Sharma SharmaSharma Chandigarh ChandigarhChandigarhChandigarh
Singhania SinghaniaSingahnia Ghaziabad GhaziabadGhaziabadGhaziabad
Raheem RaheemRaheem Lucknow LucknowLucknowLucknow

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程