·SQL的简单查询实例教程关键词:菜鸟学数据库之简单SQL语句小结为了大家更容易理解我举出的SQL语句,本文假定已经建立了一个学生成绩管理数据库,全文均以学生成绩的管理为例来描述。 : :selectnameas'姓名'fromstudentsorderbyage :selectname'姓名'fromstudentsorderbyage : :select*fromstudentswherenativein('湖南','四川') ...and:select*fromstudentswhereagebetween20and30 c.“=”:select*fromstudentswherename='李山'则说明是部分匹配,而且还有先后信息在里面,即查找以“李”开头的匹配项。所以若查询有“李”的所有对象,应该命令:'%李%';若是第二个字为李,则应为'_李%'或'_李'或'_李_'。)与"in(...)"类似,而且"[]"可以表示范围,如:select*olike'[A-C]%') :直接按照字符串处理的方式进行处理,例如:select*fromstudentswherebirth>='1980-1-1'andbirth<='1980-12-31' ()求和,如:selectcount(*)fromstudents(求学生总人数) (列)求平均,如:selectavg(mark)o=’B2’ (列)和min(列),求最大与最小 常用于统计时,如分组查总数:selectgender,count(sno)fromstudents :select*fromstudentswherenamelike'李%'(注意查询条件中有“%”, e.[]匹配检查符:select*olike'[AC]%'(表示或的关系,groupbygender(查看男女学生各有多少) 注意:从哪种角度分组就从哪列"groupby" 对于多重分组,只需将分组规则罗列。比如查询各届各专业的男女同学人数,那么分gender"selectgrade,mno,gender,count(*)fromstudentsgroupbygrade,mno,gender 通常group还和having联用,比如查询1门课以上不及格的学生,则按学号(sno)分类有:selectsno,count(*)fromgradeswheremark<60groupbysnohavingcount(*)>1 ,如:SELECT*FROMstudentsWHEREnamelike‘张%’UNION[ALL]SELECT*FROMstudentsWHEREnamelike‘李%’ ,,==o(注意可以引用别名),max(coursename),count(sno)o=o 左连接特点:显示全部左边表中的
SQL的简单查询实例教程 来自淘豆网www.taodocs.com转载请标明出处.