时间:2021-07-01 10:21:17 帮助过:15人阅读
查询语句
1 # 选择数据库 2 use credit; 3 4 # 查询语句 5 SELECT * FROM user_info limit 20; 6 7 SELECT user_id,occupation FROM user_info limit 20; 8 9 SELECT * FROM user_info 10 where sex=1 and (education=1 or marriage=1) 11 limit 20; 12 13 # 插入语句 14 insert into user_info values (‘A10‘,‘1‘,‘1‘,‘1‘,‘1‘,‘1‘); 15 insert into user_info(user_id,sex) values (‘A10‘,‘1‘); 16 17 # 记录更新 18 set sql_safe_updates = 0; 19 update user_info set occupation=‘2‘,education = ‘2‘ 20 where user_id = ‘A10‘; 21 22 # 删除记录 23 delete from user_info where user_id = ‘A10‘; 24 # delete from user_info; # 删除user_info表所有记录 25 26 # 排序 27 select * from user_info order by user_id; 28 select * from user_info order by user_id asc; # 升序排列 29 select * from user_info order by user_id desc; # 降序排列 30 select * from user_info order by sex desc,education desc; 31 select * from user_info where sex = 1 order by user_id; 32 33 # 分组 34 SELECT 35 user_id, AVG(trans_amount) 36 FROM 37 bank_detail 38 GROUP BY user_id; 39 40 SELECT 41 user_id,trans_type, AVG(trans_amount) trans_amount_avg 42 FROM 43 bank_detail 44 GROUP BY user_id,trans_type; 45 46 # 空值 47 select * from user_info where sex is null; 48 select * from user_info where sex is not null; 49 50 # 模糊匹配 51 select * from user_info where user_id like ‘278%‘; # user_id以278开头 52 select * from user_info where user_id like ‘%278%‘; # user_id包含278 53 select * from user_info where user_id like ‘%278‘; # user_id以278结尾 54 55 # 正则匹配 56 SELECT * FROM user_info WHERE user_id REGEXP ‘^278‘; # user_id以278开头 57 select * from user_info where user_id like ‘278%‘; 58 59 # 内连接 60 SELECT user_info.user_id,# 查询用户表里的 61 sex, 62 trans_amount 63 FROM user_info,bank_detail #两个表中都有要查询的字段 64 WHERE user_info.user_id = bank_detail.user_id;# 连接条件两张表上的用户ID保持一致 65 66 select count(*) from ( 67 SELECT user_info.user_id, 68 sex, 69 trans_amount 70 FROM user_info,bank_detail 71 WHERE user_info.user_id = bank_detail.user_id) a; 72 73 # 左连接 74 select count(*) from ( 75 SELECT 76 user_info.user_id, sex, trans_amount 77 FROM 78 user_info 79 LEFT JOIN 80 bank_detail ON user_info.user_id = bank_detail.user_id) a; 81 82 # 右连接 83 SELECT 84 user_info.user_id, sex, trans_amount 85 FROM 86 user_info 87 RIGHT JOIN 88 bank_detail ON user_info.user_id = bank_detail.user_id; 89 90 91 # 自定义函数 92 DROP FUNCTION if exists standardize; 93 94 DELIMITER $ 95 CREATE FUNCTION standardize(x float,y int) 96 RETURNS float 97 BEGIN 98 DECLARE a float; 99 SET a = x/5; 100 RETURN a+y; 101 END $ 102 DELIMITER ; 103 104 select standardize(3.2,3); 105 106 SELECT 107 STANDARDIZE(trans_amount,1) 108 FROM 109 bank_detail; 110 111 # 存储过程 112 drop procedure if exists info; 113 114 delimiter $ 115 create procedure info(uid int) 116 begin 117 select * from user_info where user_id = uid; 118 end $ 119 delimiter ; 120 121 call info(10); 122 123 # 重复数据 124 SELECT 125 user_id, COUNT(*) AS repetitions 126 FROM 127 bank_detail 128 GROUP BY user_id 129 HAVING repetitions > 1; 130 131 # 过滤重复 132 select count(*) from 133 (select * from bank_detail 134 group by user_id,trans_amount) a;
mysql基础小结
标签:value ble 自定义函数 exp date add mount 数据类型 join