当前位置:网站首页>Common SQL statements (complete example)
Common SQL statements (complete example)
2022-07-02 17:28:00 【Game programming】
This article is based on the school class 、 Student 、 Discipline 、 Performance and other scenes , practice SQL Use of statements
Create table
- Create class table
CREATE TABLE class(class_id INT auto_increment PRIMARY key,class_name VARCHAR(10));alter table class AUTO_INCREMENT =1;- Create student table
CREATE TABLE student(stu_id INT AUTO_INCREMENT PRIMARY KEY,stu_name VARCHAR(10),stu_sex VARCHAR(1),stu_age INT,class_id INT,foreign key(class_id) references class(class_id));alter table student AUTO_INCREMENT =1;- Create chart of accounts
CREATE TABLE course(course_id INT auto_increment PRIMARY key,course_name VARCHAR(10));alter table course AUTO_INCREMENT =1;- Create a score sheet
drop table score;CREATE TABLE score(id INT auto_increment PRIMARY key,stu_id INT,course_id INT,mark DECIMAL(3,1),foreign key(stu_id) references student(stu_id),foreign key(course_id) references course(course_id));alter table score AUTO_INCREMENT =1;desc score;Insert data into the table
The operation table is class surface
insert into class values(NULL, ' 12 ')insert into class (`class_id`,`class_name`) values (NULL, ' One 3')insert into class (`class_name`) values (' One 4')Use of stored procedures : Bulk insert data
- Create stored procedure
CREATE PROCEDURE search_sex ( #search_user_name Is the name of the stored procedure IN search_sex VARCHAR (20), # Incoming parameter OUT count_number INT # Parameters returned ) READS SQL DATA # The program contains statements to read data BEGIN SELECT COUNT(*) INTO count_number FROM student WHERE stu_sex LIKE CONCAT('%', search_sex, '%');END- Calling stored procedure
CALL search_sex(' Woman ', @nameCount);SELECT @nameCount;- Use stored procedures to insert a large amount of data into the student table
# Create stored procedures to create large amounts of data drop PROCEDURE if EXISTS insert_students;CREATE PROCEDURE insert_students(IN loop_times INT,IN stu_name CHAR,IN stu_sex CHAR,IN stu_age INT,IN class_id INT)BEGIN DECLARE var INT DEFAULT 0; WHILE var < loop_times DO INSERT INTO student VALUES (NULL,concat(stu_name,CONVERT(var,CHAR)),stu_sex,stu_age,class_id); SET var = var + 1; END WHILE;ENDCALL insert_students(10,' money ',' male ',16,4); Look for statements
- Find all grades greater than 95 The name of the classmate
SELECT student.stu_name FROM score,studentwhere score.mark>95and student.stu_id=score.stu_idGROUP BY score.stu_idhaving count(*)>1- The average query score is greater than 90 The list of students divided
SELECT score.stu_id,student.stu_name,AVG(score.mark)FROM score,student where student.stu_id=score.stu_idGROUP BY score.stu_id HAVING AVG(score.mark)>90- Find out the students with the highest average score ( What if there are two at most ??)
SELECT score.stu_id,student.stu_name,AVG(score.mark)FROM score,student where student.stu_id=score.stu_idGROUP BY score.stu_idORDER BY AVG(score.mark) desc LIMIT 1author : Test advanced
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
- Idea2021.1 installation tutorial
- ROS knowledge point - message_filters
- ROS知识点——ros::NodeHandle n 和 nh(“~“)的区别
- 深度之眼(三)——矩阵的行列式
- 云通信接口更新迭代——SUBMAIL API V4正式上线
- chmod命令原理及用法详解[通俗易懂]
- Si446 usage record (II): generate header files using wds3
- 相信自己,这次一把搞定JVM面试
- Dstat use [easy to understand]
猜你喜欢

Sword finger offer 22 The penultimate node in the linked list

Sword finger offer 27 Image of binary tree

si446使用记录(二):使用WDS3生成头文件

Microservice architecture practice: using Jenkins to realize automatic construction

Connect Porsche and 3PL EDI cases

ThreadLocal

A few lines of code to complete RPC service registration and discovery

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

Experience home office, feel the completion of the project | community essay solicitation

超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
随机推荐
畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst
Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
Sword finger offer 25 Merge two sorted linked lists
Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan
Visibilitychange – refresh the page data when the specified tab is visible
Idea2021.1 installation tutorial
Sword finger offer 26 Substructure of tree
The difference of message mechanism between MFC and QT
体验居家办公完成项目有感 | 社区征文
几行代码搞定RPC服务注册和发现
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
Update iteration of cloud communication interface -- the official launch of subail API V4
Eye of depth (II) -- matrix and its basic operations
Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
剑指 Offer 25. 合并两个排序的链表
牛客 JS3 分隔符
[shutter] dart data type (dynamic data type)
博客主题 “Text“ 夏日清新特别版