1 -- 数据库准备 2 -- 创建一个数据库 3 create database python_test charset=utf8 4 -- 使用数据库 5 use python_test 6 -- 显示当前使用的数据库是哪个 7 select database() 8 -- 创建一个数据表students 9 create table students( 10 id int unsigned not null primary key auto_increment, 11 name varchar(20) default '', 12 age tinyint unsigned default 0, 13 height decimal(5,2), 14 gender enum('男','女','保密') default '保密', 15 cls_id int unsigned default 0, 16 is_delete bit default 0 17 ); 18 -- 创建classes表 19 create table classes( 20 id int unsigned not null primary key auto_increment, 21 name varchar(30) not null 22 ); 23 -- 向students表中插入数据 24 insert into students values 25 (0,'小明',18,180.00,2,1,0), 26 (0,'小月月',18,180.00,2,2,1), 27 (0,'彭于晏',29,185.00,1,1,0), 28 (0,'刘德华',59,175.00,1,2,1), 29 (0,'黄蓉',38,160.00,2,3,0), 30 (0,'凤姐',28,150.00,3,2,1), 31 (0,'王祖贤',18,172.00,2,1,1), 32 (0,'周杰伦',36,NULL,1,1,0), 33 (0,'程坤',27,181.00,1,2,0), 34 (0,'周杰伦',36,NULL,1,1,0), 35 (0,'刘亦菲',25,166.00,2,2,0), 36 (0,'静香',12,180.00,2,4,0), 37 (0,'郭靖',12,170.00,1,4,0), 38 (0,'周杰',34,176.00,2,5,0); 39 -- 向classes中插入数据 40 insert into classes values 41 (0,'python_01期'), 42 (0,'python_02期'), 43 (0,'python_03期'); 44 45 46 -- 查询 47 -- 查询所有字段 48 -- select * from 表名; 49 select * from students; 50 select * from classes; 51 52 -- 查询指定字段 53 -- select 列1,列2... from 表名; 54 select name,age from students; 55 56 -- 使用as给字段起别名 57 -- select 字段 as 别名... from 表名; 58 select name as 姓名,age as 年龄 from students; 59 60 -- select 表名.字段... from 表名; 61 select students.name, students.age from students; 62 63 -- 可以通过as 给表起别名 64 -- select 别名.字段... from 表名 as 别名; 65 select s.name, s.age from students as s; 66 67 -- 消除重复行 68 -- distinct 字段 69 select distinct gender from students; 70 71 -- 条件查询 72 -- 比较运算符 73 -- select ... from 表名 where... 74 -- > 75 -- 查询大于18岁的信息 76 select * from students where age>18; 77 78 -- < 79 -- 查询小于18岁的信息 80 select * from students where age<18; 81 82 -- >= 83 -- <= 84 -- 查询小于或等于18岁的信息 85 select * from students where age<=18; 86 87 -- != 或者<> 88 -- 查询年龄不等于18岁的消息 89 select * from students where age!=18; 90 91 -- 逻辑运算符 92 -- and 93 -- 18到28之间的所有学生信息 94 select * from students where age>18 and age<28; 95 -- 失败 select * from students where age>18 and <28; 96 97 -- or 98 -- 18以上或者身高超过180(包含) 99 select * from students where age>18 or height>=180; 100 101 -- not 102 -- 不是 18岁以上的女性 103 select * from students where not (age>18 and gender=12); 104 105 -- 年龄不是小于或等于18 并且是女性 106 select * from students where (not age<=18) and gender=2; 107 108 -- 模糊查询 109 -- like 110 -- % 替换1个或者多个 111 -- _ 替换1个 112 -- 查询姓名中以‘小’开始的名字 113 select * from students where name like '小%'; 114 115 -- 查询姓名中有‘小’的所有名字 116 select name from students where name like '%小%'; 117 118 -- 查询有2个字的名字 119 select name from students where name like '__'; 120 121 -- 查询至少有2个字的名字 122 select name from students where name like '__%'; 123 124 -- rlike 正则 125 -- 查询以 周开始的姓名 126 select name from students where name rlike '^周.*'; 127 128 -- 查询以周开始、伦结尾的姓名 129 select name from students where name rlike '^周.*伦$'; 130 131 -- 范围查询 132 -- in(1, 3, 8)表示在一个非连续的范围内 133 -- 查询 年龄为12,18,34的姓名 134 select name from students where age in (12,18,34); 135 136 -- not in 不非连续的范围内 137 -- 年龄不是18,34之间的信息 138 select name from students where age not in (18,34); 139 140 -- between ... and ... 表示在一个连续的范围内 141 -- 查询年龄在18到34之间的信息 142 select name from students where age between 18 and 34; 143 144 -- not between ... and ... 表示不在一个连续的范围内 145 -- 查询年龄不在18到34之间的数据 146 select name from students where age not between 18 and 34; 147 -- 失败 select name from students where age not (between 18 and 34); 148 149 -- 空判断 150 -- 判空 is null 151 -- 查询身高为空的信息 152 select name from students where height is null; 153 154 -- 判非空 is not null 155 156 -- 排序 157 -- order by 字段 158 -- asc从小到大排序 即升序 159 -- desc 从大到小排序 即降序 160 -- 查询年龄在18到34岁之间的男性,按照年龄从小到大排序 161 select name from students where (age between 18 and 34) and gender=1 order by age asc 162 163 -- 查询年龄在18岁到34岁之间的女性,身高从高到矮排序 164 select name from students where (age between 18 and 34) and gender=2 order by height desc 165 166 -- order by 多个字段 167 -- 查询年龄在18到34岁之间的女性,身高从高到矮排序,如果身高相同的情况下按照年龄从小到大排序 168 select name from students where (age between 18 and 34) and gender=2 order by height desc,age asc; 169 170 -- 查询年龄在18到34岁之间的女性,身高从高到矮排序,如果身高相同的情况下按照年龄从小到大排序 171 -- 如果年龄也相同那么按照id从大到小排序 172 select name from students where (age between 18 and 34) and gender=2 order by height desc,age asc,id desc; 173 174 -- 按照年龄从小到大、身高从高到矮的排序 175 select * from students order by age asc,height desc; 176 177 -- 聚合函数 178 -- 总数 179 -- count 180 -- 查询男性有多少人,女性有多少人 181 -- select count(*) from students where gender=1; 182 select count(*) as 男性人数 from students where gender=1; 183 select count(*) as 女性人数 from students where gender=2; 184 185 -- 最大值 186 -- max 187 -- 查询最大的年龄 188 select max(age) from students; 189 190 -- 查询女性的最高 身高 191 select max(height) from students where gender=2; 192 193 -- 最小值 194 -- main 195 select main(height) from students; 196 197 -- 求和 198 -- sum 199 -- 计算所有人的年龄总和 200 select sum(age) from students; 201 202 -- 平均值 203 -- avg 204 -- 计算平均年龄 205 select avg(age) from students; 206 207 -- 计算平均年龄 sum(age)/count(*) 208 select sum(age)/count(*) from students; 209 210 -- 四舍五入 round(123.23, 1) 保留1位小数 211 -- 计算所有人的平均年龄,保留2位小数 212 select round(avg(age), 2) from students; 213 214 -- 计算男性的平均身高 保留2位小数 215 select round(avg(height), 2) from students where gender=1; 216 217 -- 分组 218 -- group by 219 -- 按照性别分组,查询所有的性别 220 select gender from students group by gender; 221 222 -- 计算每种性别中的人数 223 select gender,count(*) from students group by gender; 224 225 -- 计算男性的人数 226 select gender,count(*) from students where gender=1 group by gender; 227 228 -- group_count(...) 229 -- 查询同种性别中的人名 230 select gender,group_count(name) from students group by gender; 231 -- group_count内多种参数,用符号分隔。查询同性别的人名、年龄和id 232 select gender,group_countn(name, '_' , age, '_' ,id) from students group by gender_count; 233 234 -- having 235 -- 查询平均年龄超过30岁的性别,以及姓名 having avg(age)>30 236 select gender,group_count(name) from students group by gender having avg(age)>30; 237 238 -- 查询每种性别中人数大于2的性别及姓名 239 select gender,group_count(name) from students group by gender having count(*)>2; 240 241 -- 分页 242 -- limit start, count 243 244 -- 限制查询出来的数据个数 245 select * from students where gender=1 limit 2; 246 247 -- 查询前5个数据 248 select * from students limit 0, 5; 249 250 -- 查询id 6-10(包含)的数据 251 select * from students limit 5, 5; 252 253 -- 每页显示2个,显示第1页数据 254 select * from students limit 0, 2; 255 256 -- 每页显示2个,显示第2页数据 257 select * from students limit 2, 2; 258 259 -- 每页显示2个,显示第3页数据 260 select * from students limit 4, 2; 261 262 -- 每页显示2个,显示第4页数据 263 select * from students limit 6, 2; 264 265 -- 每页显示2个,显示第6页的信息,按照年龄从小到大排序 266 select * from students limit 10, 2; 267 -- 失败select * from students order by age limit (6-1)*2, 2 268 269 -- 连接查询 270 -- 内连接 271 -- inner join ... on 272 -- 查询 有能够对应班级的学生以及班级信息 273 select * from students inner join classes on students.cls_id=classes.id 274 275 -- 按照要求显示姓名、班级 276 select students.name,classes.name from students inner join classes on students.cls_id=classes.id; 277 278 -- 给数据表起名字 279 select s.name,c.name from students as s inner join classes as c on s.cls_id=c.id; 280 281 -- 查询 有能够对应班级的学生以及班级信息,显示学生的所有信息,只显示班级名臣 282 select s.*,c.name from students as s inner join classes as c on s.cls_id=c.id; 283 284 -- 在以上的查询中,将班级姓名显示在第1列 285 select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id; 286 -- select c.name,s.* from classes as c inner join students as s on c.id=s.cls_id; 287 288 -- 查询 有能够对应班级的学生以及班级信息,按照班级进行排序 289 select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name; 290 291 -- 当是同一个班级的时候,按照学生的id从小到大排序 292 select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id; 293 294 -- 左连接 295 -- left join 296 -- 查询每位学生对应的班级信息 297 select * from students as s left join classes as c on s.cls_id=c.id; 298 299 -- 查询没有对应班级信息的学生 300 -- select ... from xxx as s left join xxx as c on ... where ... 301 -- select ... from xxx as s left join xxx as c on ... having ... 302 select * from students as s left join classes as c on s.cls_id=c.id where c.id is null; 303 select * from students as s left join classes as c on s.cls_id=c.id having c.id is null; 304 305 -- right join on 306 -- 将数据表名字互换位置,用left join完成 307 308 -- 自关联 309 -- 导入sql语句 310 -- sql语句文件所在目录,进入数据库(use 数据库名)。 source sql文件名 即可导入 311 source areas.sql 312 -- 省级联动 http://demo.lanrenzhijia.com/2014/city0605/ 313 314 -- 查询所有省份 315 select * from areas where pid=0; 316 317 -- 查询出江西省有哪些市 318 select * from areas as province inner join areas as city on city.pid=province.id having province.atitle='江西省'; 319 -- 只显示省份和市的名称 320 select province.atitle,city.atitle from areas as province inner join areas as city on city.pid=province.id having province.atitle='江西省'; 321 322 -- 查询出景德镇市有哪些县城 323 select province.atitle,city.atitle from areas as province inner join areas as city on city.pid=province.id having province.atitle='景德镇市'; 324 325 -- 子查询 326 -- 标量子查询 327 -- 查询出高于平均身高的信息 328 329 -- 查询最高的男生信息 330 select * from students where height=(select max(height) from students); 331 332 -- 列级子查询 333 -- 查询学生的班级号能够对应的学生信息 334 select * from students where cls_id in (select id from classes);
查询的完整格式:
SELECT select_expr [,select_expr,...] [ FROM tb_name [WHERE 条件判断] [GROUP BY {col_name | postion} [ASC | DESC], ...] [HAVING WHERE 条件判断] [ORDER BY {col_name|expr|postion} [ASC | DESC], ...] [ LIMIT {[offset,]rowcount | row_count OFFSET offset}] ]