MySQL中select语句
来源:互联网 发布:斗鱼刷人气软件那个好 编辑:程序博客网 时间:2024/05/09 21:31
数据表都已经创建起来了,假设我们已经插入了许多的数据,我们就可以用自己喜欢的方式对数据表里面的信息进行检索和显示了,比如说:可以象下面这样把整个数据表内的内容都显示出来
1、用各种操作符来设定检索条件
select * from score where score>95; //显示所有分数在95分以上的信息
select last_name,first_name from president where last_name=’Tom’; //找出所有姓tom的总统
select last_name,first_name,birth,state from president
where birth<’1950-1-1’ and (state=’VA’ or state=’BA’);
2、NULL 值的特别处理
select last_name,first_name,birth,state from president
where death is null; //找出所有没死的总统
3、查询结果进行排序
select last_name,first_name,birth,state from president
order by last_name; //让总统们的名字按字母顺序排列
select last_name,first_name from president
order by state DESC,last_name ASC;
4、限制查询结果中数据行个数
select last_name,first_name,birth,state from president
order by birth limit 5;
order by birth limit 10,5; //返回从第11个记录开始的5个记录(跳过了10个)
select last_name,first_name,birth,state from president
order by rand() limit 1; //这是用了表达式求值的方法,在哪里都管用
5、对输出列进行求值和命名
Select 17,format(sqrt(3*3+4*4),0))
select concat(first_namem,’ ‘,last_name),concat(city,’,’,state) from president;
如果合并之后输出列的标题过长,则可以给其一个别名,如:
select concat(first_namem,’ ‘,last_name) as name,
concat(city,’,’,state) as birth place
from president;这样就比较美观了。
6、和日期有关的问题
select * from event where date=’2002-10-01’ //看看这天有何考试信息?
select last_name,first_name,birth,state from president
where death>’1900-01-01’ and death<’2000-01-01’; //看看上个世纪死了几个?
select last_name,first_name,birth from president
where month(birth)=3; //谁生在3月 ?
where month(birth)=7 and dayofmonth(birth) =6; //谁生在7月6日?(汤姆克鲁斯?)
select last_name,first_name,birth to_days(death)-to_days(birth) as age from president
select last_name,first_name,expiration from member
where (to_days(expiration)-to_days(curdate())<60; //有些人60天内需要花钱了!
7、模式匹配
select concat(first_namem,’ ‘,last_name) as name,
where last_name like ‘W%’; //找到以W或w开头的人
where last_name like ‘%W%’; //找到名字里面W或w开头的人
8、设置和使用SQL变量
select @birth:=birth from president
where last_name =’adsltiger’;
select concat(first_namem,’ ‘,last_name) as name from president
where birth<@birth order by birth; //看看那些人比我大!
*9、生成统计信息
9.1 找出一组数据中到底有多少种不同的值是一项比较常见的统计工作,而关键字distinct就可以把查询结果中的重复数据清除掉。如
9.2用count()函数来统计相关记录的个数,注意其使用方法:count(*)计算所有的,NULL也要;count(数据列名称) NULL值不计算在内。
select count(*) from president;
9.3如果我们想知道班级内的男女生数目?该如何查询呢?最简单的方法是
select count(*) from student where sex=’f’;
select count(*) from student where sex=’m
select sex,count(*) f rom student group by sex;
select state,count(*) as count from president
group by state order by count desc limt4; //看看出生总统最多的前四个州是哪几个?
9.4除了count(),我们还用其他一些统计函数,如求出最小值的min(),求最大值的max(),求和的sum(),求平均值的avg(),在实际工作中,这些函数时经常用到的!
*10、从多个表提取信息
例子1:查询某一天内的学生们的考试成绩,用学号列出。
select scroe.student_id,event_date,score.score.event.type
from event,score
where event.date=’2003-09-12’
and event.event_id=score.event_id
例子2:查询某一天内的学生们的考试成绩,用姓名列出。
select student.name event.name,score.score,event.type
form event,score,student
where event.date=’2003-09-12’
and event.event_id= score.event_id
and scroe.student_id=student.student_id;
例子3:查询一下缺席学生的名字,学号,缺席次数
select student.student_id,student_name
count(absence.date) as absences
from student,absence
where student.student_id=absence.student_id
group by student.student_id;
- MySQL中select语句
- MySQL中select语句详解
- 快速掌握MySQL数据库中SELECT语句
- MySQL中SELECT语句的基本语法
- 快速掌握MySQL数据库中SELECT语句
- Mysql Select 语句中实现的判断
- Mysql Select 语句中实现的判断
- MySQL中SELECT语句的执行顺序
- Mysql 语言中select语句的总结
- mysql中SELECT语句的执行顺序
- Mysql Select 语句中实现的判断
- Mysql Select 语句中实现的判断
- Mysql Select 语句中实现的判断
- MySQL-SQL语句中SELECT语句的执行顺序
- MySQL-SQL语句中SELECT语句的执行顺序
- mysql的select语句
- mysql的select语句
- mysql select 语句综述
- 读取CLASS文件JDK版本
- C++ Templates FAQ
- base标签与a标签的href属性
- 去掉PowerDesigner生成SQL脚本中字段名带的引号
- 黑马程序员-AWT编程
- MySQL中select语句
- UVa 10055 Hashmat the brave warrior(勇士)
- java 父类子类静态成员,实例成员,构造函数初始化的顺序
- 多媒体函数简介
- 使用VS2008下调试SGI STL源码
- 【解决Win8系统磁盘占用100%的方法】
- VS2012开发PHP,Linux Server运行PHP,支持远程调试
- Android中解析XML
- hdu1301 Jungle Roads