查询全部数据 Select * from 表名; Select * from stu; 根据条件查询指定的数据 Select * from 表名 where 列名1=值 and 列名2=值.... Select * from stu where sid=9 and ssex='女'; 查询数据,返回指定的列 Select 列名1,列名2 from stu; Select sid,sname from stu; 给指定返回列取别名(小名) 两种方式: Select 列名 别名,列名2 别名2... from 表名; Select 列名 as 别名,列名2 as 别名2... from 表名; Select sid 学号,sname 姓名,ssex 性别 from stu; Select sid as 学号,sname as 姓名,ssex as 性别 from stu; 在条件中使用比较运算符 SELECT * FROM 表名 where 字段 > < >= <= !=或<> select * from j18 where xsnianling !=18 多条件的查询: AND OR NOT select * from j18 where xsnianling <=21 and xsxingbie='女' select * from j18 where xsnianling <21 or xsxingbie='女' select * from j18 where xsnianling not in(18,21,25) 对空值的查询:is null 对应列是否null查询 select * from j18 where xsxueli is not null select * from j18 where xsxueli is null BETWEEN A AND B 在A和B之间,包含AB的值 select * from j18 where xsnianling BETWEEN 18 and 21 IN select * from j18 where xsnianling in(18,21,25) 模糊查询 LIKE %:指代不明确值的位置或长度 _:指代明确值的位置或已知字符串长度 select * from j18 where xsxingming like '_灵%' 查询中使用算术表达式:+ - * / select xsxuehao+xsnianling from j18 where xsxingming like '_灵%' 处理重复值:DISTINCT 排除重复展示,只展示一次 select DISTINCT xsxingbie from j18; 查询返回限定行数:LIMIT Limit 10 取查询数据的前10位 Limit 10,10 从查询数据的第10位开始,向后取10位数据展示,不满足10位也不会报错 通过查询复制表 create table stu1 select * from stu; --只复制结构 create table stu2 select * from stu where 1=2; 分组 group by select ssex,COUNT(*) from stu GROUP BY ssex 分组使用的时候,,group by 字段,一定要在 select 后面出现,如果使用了group by select 后面就不要出现 * 排序 order by 字段名 :字段名就是我们需要排序的字段 order by xsnianling 升序 默认 order by xsnianling desc 降序 |