当前位置:网站首页>MySQL course 2. Various queries of tables
MySQL course 2. Various queries of tables
2022-07-27 02:21:00 【People run faster than code】
First put a table for operation :
/*
Navicat Premium Data Transfer
Source Server : mysql
Source Server Type : MySQL
Source Server Version : 50732
Source Host : rm-bp1zq3879r28p726lco.mysql.rds.aliyuncs.com:3306
Source Schema : mytest
Target Server Type : MySQL
Target Server Version : 50732
File Encoding : 65001
Date: 28/06/2022 20:17:27
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_users
-- ----------------------------
DROP TABLE IF EXISTS `t_users`;
CREATE TABLE `t_users` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`sex` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`id_no` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`age` int(11) NOT NULL,
`type` int(8) NOT NULL,
`use_date` datetime(0) NOT NULL,
`use_state` int(1) NOT NULL,
`createDate` datetime(0) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `name_index`(`name`) USING BTREE,
INDEX `name`(`name`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 44 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of t_users
-- ----------------------------
INSERT INTO `t_users` VALUES (1, ' Zhang San er', ' Woman ', '45001001001201001', 21, 2, '2022-05-18 08:59:46', 1, '2022-05-12 08:59:46');
INSERT INTO `t_users` VALUES (2, 'I like Three sheets er', ' male ', '45001001001001001', 20, 1, '2022-05-18 09:00:27', 0, '2022-05-13 09:00:27');
INSERT INTO `t_users` VALUES (3, ' Three sheets ', ' Woman ', '45001001231001021', 52, 1, '2022-05-18 09:01:10', 1, '2022-05-16 09:01:10');
INSERT INTO `t_users` VALUES (4, ' Three sheets er', ' Woman ', '45001001001001221', 30, 2, '2022-05-18 09:01:41', 1, '2022-05-18 09:01:41');
INSERT INTO `t_users` VALUES (5, ' Three sheets ', ' Woman ', '45001001101001221', 16, 1, '2022-05-18 09:02:41', 1, '2022-05-18 09:02:41');
INSERT INTO `t_users` VALUES (6, ' Zhang er', ' Woman ', '45001001201001221', 18, 2, '2022-05-18 09:02:42', 1, '2022-05-18 09:02:42');
INSERT INTO `t_users` VALUES (9, ' Three sheets er', ' Woman ', '45015555245225622166', 25, 1, '2022-06-26 14:25:18', 1, '2022-06-26 14:25:22');
INSERT INTO `t_users` VALUES (11, ' Three sheets ', ' male ', '45234785758798374', 35, 0, '2022-06-26 14:31:20', 0, '1612-07-14 14:31:35');
INSERT INTO `t_users` VALUES (12, ' Zhang San, Zhang San ', ' male ', '4502215564123344', 18, 2, '2022-06-26 14:38:54', 0, '2022-06-26 14:39:01');
INSERT INTO `t_users` VALUES (13, ' Zhang San ', ' Woman ', '4520123454646746', 15, 2, '2022-06-26 14:39:27', 1, '2022-06-26 14:39:31');
INSERT INTO `t_users` VALUES (14, ' Zhang San ', ' male ', '4588228592659599', 18, 0, '2022-06-26 14:41:17', 3, '2022-07-08 14:41:21');
INSERT INTO `t_users` VALUES (15, ' Zhang San * Zhang San, Zhang San ', ' Woman ', '452124200201122523', 33, 2, '2022-06-18 14:43:13', 3, '0000-00-00 00:00:00');
INSERT INTO `t_users` VALUES (20, ' Yin Zhangsan ', ' male ', '45456544654745645', 30, 1, '2022-06-26 15:07:20', 1, '2022-06-26 15:07:37');
INSERT INTO `t_users` VALUES (21, ' Zhang San, Zhang San ', ' male ', '45456544654745645', 30, 1, '2022-06-26 15:07:20', 1, '2022-06-26 15:07:37');
INSERT INTO `t_users` VALUES (28, ' One eye opens three ', ' male ', '848778', 20, 1, '2022-06-26 15:29:00', 0, '0000-00-00 00:00:00');
INSERT INTO `t_users` VALUES (29, ' Two eyes open three ', ' male ', '99', 21, 1, '2022-06-26 15:33:08', 1, '2022-06-26 15:33:08');
INSERT INTO `t_users` VALUES (30, ' All eyes are on Zhang San !', ' male ', '108108413', 13, 1, '2022-06-26 15:33:58', 1, '2022-06-26 15:33:58');
INSERT INTO `t_users` VALUES (31, ' I am a two thorn newt ', ' male ', '99', 21, 1, '2022-06-26 15:35:12', 1, '2022-06-26 15:35:12');
INSERT INTO `t_users` VALUES (32, ' What are you doing? ~ Ouch! ~', ' Woman ', '114514', 66, 1, '2022-06-26 15:35:23', 1, '2022-06-26 15:35:27');
INSERT INTO `t_users` VALUES (33, ' Four eyes open three ', ' male ', '99', 21, 1, '2022-06-26 15:35:38', 1, '2022-06-26 15:35:38');
INSERT INTO `t_users` VALUES (34, ' Five eyes open three ', ' male ', '99', 21, 1, '2022-06-26 15:35:36', 0, '0000-00-00 00:00:00');
INSERT INTO `t_users` VALUES (35, ' I'm Zhang San , You are presumptuous ', ' male ', '99', 21, 1, '2022-06-26 15:36:31', 1, '2022-06-26 15:36:31');
INSERT INTO `t_users` VALUES (36, ' I'm Zhang San , You are presumptuous ', ' Woman ', '995465466545', 21, 1, '2022-06-26 15:38:16', 1, '2022-06-26 15:38:16');
INSERT INTO `t_users` VALUES (37, ' Mona Zhang San ', ' Woman ', '10001110101010', 99, 1, '1793-09-02 08:25:16', 0, '0211-07-08 15:14:00');
INSERT INTO `t_users` VALUES (38, ' Real Zhu Zhangsan ', ' Woman ', '123457', 20, 1, '2022-06-26 15:44:01', 1, '2022-06-26 15:44:01');
INSERT INTO `t_users` VALUES (39, ' Zhang San ', ' male ', '543212345', 99, 2, '2022-06-26 15:44:02', 1, '2022-06-26 15:44:02');
INSERT INTO `t_users` VALUES (40, ' Three for three ', ' male ', '108108413', 13, 1, '2022-06-26 15:44:03', 1, '2022-06-26 15:44:03');
INSERT INTO `t_users` VALUES (41, ' Zhang San, I love you !', ' Woman ', '4545156', 60, 0, '0000-00-00 00:00:00', 0, '0000-00-00 00:00:00');
INSERT INTO `t_users` VALUES (42, 'woli Zhang saner ', ' male ', '123458888', 16, 0, '2022-06-22 16:23:58', 0, '2022-06-07 16:24:07');
INSERT INTO `t_users` VALUES (43, ' If it's Zhang San , Yes ', ' male ', '5201314', 18, 0, '2022-06-28 19:12:37', 0, '2022-06-28 19:12:39');
SET FOREIGN_KEY_CHECKS = 1;
#DQL Statement exercise table t_users
#where
-- select * from t_users where sex=' male ';
# Relationship >=< Logic and or
-- select * from t_users where id<=5;
-- select * from t_users where age=16 and type=1;
-- select * from t_users where id=6 or id=5;
# The number ( Including time ) Range queries between...and... Include boundary values , Just one more equal sign
-- select * from t_users where age between 20 and 30;
-- select * from t_users where createDate between
-- '2022-5-12 09:00:00' and '2022-5-18 14:28:50';
# Fuzzy query like 【_】 An underscore represents only one Chinese / character ,【%】 wildcard
-- select * from t_users where name like '%er%';
-- select * from t_users where name like '_er%';
# Clear duplicate values distinct
-- SELECT distinct age from t_users;
#in contain not in It doesn't contain
-- select * from t_users where id in (1,5,8,9,3);
-- select * from t_users where id not in (1,5,8,9,3);
# Group query GROUP BY Will be used with aggregate functions
-- select sex,count(sex) ' Number ' from t_users GROUP BY sex;
#order by Sort positive sequence asc flashback desc
-- select * from t_users ORDER BY createDate desc;
#limit Pagination Query the data subscript , Starting from scratch , If it is self increasing, it is 1 Start
-- SELECT * from t_users LIMIT 14,6;
#as anonymous
-- select *,name as' full name ',' Query finished 'as' remarks ' from t_users;
-----------------------
# Aggregate functions max min count avg sum
-- select
-- max(age) ' The oldest person ', -- min(age) ' The youngest ',
-- count(id) ' The total number of ',
-- avg(age) ' Average age ',
-- sum(age) ' Age sum '
-- from t_users;
# Query the information of the oldest and youngest people
-- select *,' remarks ' ' Information about the oldest person ' from t_users
-- where age=(select max(age) from t_users);
--
-- select *,' remarks ' ' Information about the youngest person ' from t_users
-- where age=(select min(age) from t_users);
# Group by sex , And check the corresponding number
-- select sex,count(sex) ' The number of ',
-- sum(age)' Total age ',
-- avg(age)' The average age of men '
-- from t_users GROUP BY sex;
# Index query test
# There is no quotation 1.3 About seconds
-- SELECT * from test_table where introduce='mei5mei';
# Use index 0.05 About seconds
-- SELECT * FROM test_table WHERE name='shuai992';
# The cartesian product
-- SELECT * from
-- users left JOIN user_address
-- on
-- users.id=user_address.uid;
/*
Navicat Premium Data Transfer
Source Server : mysql
Source Server Type : MySQL
Source Server Version : 50732
Source Host : rm-bp1zq3879r28p726lco.mysql.rds.aliyuncs.com:3306
Source Schema : mytest
Target Server Type : MySQL
Target Server Version : 50732
File Encoding : 65001
Date: 28/06/2022 20:40:34
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`createDate` datetime(0) NOT NULL,
`userName` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`passWord` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`age` int(3) NOT NULL,
`phone` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`introduce` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`sex` int(250) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `userName_index`(`userName`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (1, '2022-06-26 13:43:11', 'admin', '123456', 20, '15912345678', 'jiojio shake ', 1);
INSERT INTO `users` VALUES (2, '2022-06-26 13:43:11', 'zhangsan', '123456', 30, '15912345678', ' I love cooking .', 1);
INSERT INTO `users` VALUES (3, '2022-06-26 13:43:11', 'lisi', '45451', 0, '15912345678', ' Great danger Tianlong ', 1);
INSERT INTO `users` VALUES (4, '2022-06-26 13:43:11', 'zhaoliu', '2222', 49, '15912345678', ' Like martial arts .', 1);
INSERT INTO `users` VALUES (5, '2022-06-26 13:43:11', ' Zhang San ', '11111', 16, '15912345678', ' Like dancing .', 0);
INSERT INTO `users` VALUES (6, '2022-06-26 13:43:11', 'ikun', '123787', 67, '15912345678', 'sb', 2);
INSERT INTO `users` VALUES (7, '2022-06-28 19:15:47', '???', '12112', 99, '15685465', ' Confess Chen ', 0);
INSERT INTO `users` VALUES (8, '2022-06-28 20:39:01', ' Celia ', '88888888', 100, '11111111111', ' Hello, my name is Celia ', 0);
SET FOREIGN_KEY_CHECKS = 1;
Put the second operation table
-- create view newTableName as( # Create view
-- select state ' state ',count(state)
-- from
-- (
-- select userName ,age,
-- (
-- case
-- when age<20 then ' youth '
-- when age<39 then ' Prime of life '
-- else ' middle-aged '
-- end
-- )'state'
-- from users
-- )result GROUP BY state;
-- select state ' state ',count(state) ' Number '
-- from newTableName GROUP BY state;
# Create stored procedure
delimiter $$
create procedure mypro233()
BEGIN
update users set introduce=' Beggar brother ' where userName='ikun';
end
$$
delimiter ;
# Use !
call mypro233;
delimiter $$ # Flexible stored procedures
create procedure nmmdw(in new_id int)
BEGIN
update users set introduce=' Chicken, you are so beautiful ' where id=new_id;
end
$$
delimiter ;
call nmmdw(6);
边栏推荐
- Lora communication application development
- First knowledge of C language (2)
- 识时务者常用网址大全
- 多线程中 synchronized 锁升级的原理是什么?
- Nb-iot access to cloud platform
- Ogeek meetup phase I, together with cubefs, is hot
- OSPF的重发布及路由策略
- 定时器中断实验
- Mechanical hard disk Selection Guide -- from the selection experience
- Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan
猜你喜欢

The basic configuration of static routing (planning of IP address and configuration of static routing) realizes the accessibility of the whole network.

C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
npm报错, Error: EPERM: operation not permitted, mkdir

记录第N次SQL异常

OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)

TCP的三次握手与四次挥手(简述)

(title + detailed idea + annotated code) codeforces round 805 (Div. 3) F Equate Multisets
![[explain C language in detail] this article takes you to know C language and makes you impressed](/img/37/205c1c6eb2ba704941e48ff89c6268.png)
[explain C language in detail] this article takes you to know C language and makes you impressed

(史上最详细)Codeforces Round #805 (Div. 3)E. Split Into Two Sets

Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan
随机推荐
(prefix and / thinking) codeforces round 806 (Div. 4) F Yet Another Problem About Pairs Satisfying an Inequality
6.30 didi surface warp (one side + two sides)
ospf协议概述以及基础概念
Lvs+keepalived project practice
OSPF在MGRE环境下的实验
HCIA (network elementary comprehensive experimental exercise)
STM32入门教程第一讲
mgre的全连和星型拓扑实验
NB-IOT联网通信
[explain C language in detail] this article takes you to know C language and makes you impressed
6.28 Dahua written examination
Lora通信应用开发
C语言——二维数组、指针
JVM面试题(面试必备)
Simple application of rip V2 (V2 configuration, announcement, manual summary, ripv2 authentication, silent interface, accelerating convergence)
Mechanical hard disk Selection Guide -- from the selection experience
C语言——字符和字符串、算术运算符、类型转换
Codeforces Round #807 (Div. 2), problem: (C) Mark and His Unfinished Essay
ESP8266Wi-Fi接入云平台
MySQL课程2.表的各种查询