-------------------------查询(查)-----------------------------------
--14、查询的语法:
--select 列名1, 列名2....------》查询所有字段用“*”代替
--from 表名
--[where 条件]
--[order by 排序字段 asc 或 desc,默认是升序]
--基本格式一:省略where和order by的情况
--例:查询学生表中的所有信息
select * from stuinfo
--例:查询学生表中的学生学号和姓名
select stuno,stuname from stuinfo
--基本格式二:带where条件
--例:查询学生表中所有上海的男生
select * from stuinfo where addr = '上海' and sex = '男'
--基本格式三:带where条件和order by排序
--例:查询学生表中有地址的学生,并按照年龄从大到小排序
select * from stuinfo
where addr is not null and addr <> ''
order by age desc
--组合排序
--例:查询学生表中有地址的学生,并先按照年龄从大到小排序,如果年龄一样的再按照学号由大到小排序
select * from stuinfo
where addr is not null and addr <> ''
order by age asc,stuno desc
--联系:查询学生表中地址不在北京和深圳的学生,并先按照年龄小到大排序
select * from stuinfo
where addr is null or addr not in('北京','深圳')
oder by age asc
--15、列名和表名去别名,注意:别名不影响表真正的名字
--格式一:列名 as 别名
--例:查询学生表中的学生学号和姓名
select stuno as '学号',stuname as '姓名' from stuinfo
--格式二:省略as,列名 别名
--例:查询学生表中的学生学号和姓名
select stuno '学号',stuname '姓名' from stuinfo
--格式三:别名=列名
--例:查询学生表中的学生学号和姓名
select '学号'=stuno ,'姓名' = stuname from stuinfo
--格式四:列名取别名是合并多个列,(列名 + 列名) 别名,显示是可以增加其他字符
select stuno '学号',stuname '姓名' , sex +'|| '+ email '其他信息'
from stuinfo
--表名也可以取别名
select A.stuno '学号',A.stuname '姓名'
from stuinfo A
--常量列:增加某个列取固定的数据,仅用于显示
select stuno '学号',stuname '姓名', '软件测试' as '课程'
from stuinfo
--16、空值的查询:列名 is (not) null
--例:查询没有地址的学生信息
select * from stuinfo
where addr is null or addr = ''
--例:查询地址存在的学生信息
select * from stuinfo
where addr is not null and addr <> ''
--错误情况:
select * from stuinfo where addr = null------>错误的
select * from stuinfo where addr = 'null' --->错误的
select * from stuinfo where ad