您的位置:首页 > 数据库 > MySQL

mysql入门学习之一:查询

2012-05-31 16:13 441 查看
查询数据范例:

create table student (
id INT(10) NOT NULL UNIQUE PRIMARY  KEY,
name VARCHAR(20) NOT NULL,
sex VARCHAR(4),
birth YEAR,
department VARCHAR(20),
address VARCHAR(50)
);

create table score (
id INT(10) NOT NULL UNIQUE PRIMARY KEY  AUTO_INCREMENT,
stu_id INT(10) NOT NULL,
c_name VARCHAR(20),
grade INT(10)
);

insert into student  VALUES (901,'张老大','男','1985','计算机系','北京市海淀区');
insert into student  VALUES (902,'张老二','男','1986','中文系','北京市昌平区');
insert into student  VALUES (903,'张三','女','1990','中文系','湖南省永州市');
insert into student  VALUES (904,'李四','男','1990','英语系','辽宁省阜新市');
insert into student  VALUES (905,'王五','女','1991','英语系','福建省厦门市');
insert into student  VALUES (906,'王六','男','1988','计算机系','湖南省衡阳市');

insert into  score   VALUES  (NULL,901,'计算机','98');
insert into  score   VALUES  (NULL,901,'英语','80');
insert into  score   VALUES  (NULL,902,'计算机','65');
insert into  score   VALUES  (NULL,902,'中文','88');
insert into  score   VALUES  (NULL,903,'中文','85');
insert into  score   VALUES  (NULL,903,'计算机','98');
insert into  score   VALUES  (NULL,904,'计算机','70');
insert into  score   VALUES  (NULL,904,'英语','92');
insert into  score   VALUES  (NULL,905,'英语','94');
insert into  score   VALUES  (NULL,906,'计算机','90');
insert into  score   VALUES  (NULL,906,'英语','85');


列表查询
select * from student  where  department IN  ('计算机系','英语系');


按条件查询
select * from student  where  department='计算机系'  or  department='英语系';


查询范围

select * from student where 2009-birth between 18 AND 22;
select * FROM student where 2009-birth>=18 AND 2009-birth<=22;


分组查询 group by

select department, COUNT(id) from  student  group by  department;
select department, COUNT(id) AS sum_info  from  student  group by  department;

说明一:如果在一个表里N个用户每个用户有多个收入,需要计算group by
select name,SUM(num) from test group by name;
说明二:计算每个用户信息出现的次数
select name,count(num) from test group by name


分组查询最大值 在所有的c_name中,grade列中最大的一个,即使一个c_name有多个值
select c_name, max(grade) from score group by c_name;


两张表查询
select c_name,grade from score
where stu_id = (select id from student where name='李四');

两张表联合查询
select* from student,score where student.id=score.stu_id;
或者
select * from student inner join score where student.id=score.stu_id ;

左联结
select from student LEFT JOIN score ON student.id=score.stu_id
右连结
select from student JOIN score ON student.id=score.stu_id

SUM数据统计
select stu_id, SUM(grade)
from score
group by stu_id;

两张表集合统计
select stu_id, SUM(grade) from score,student
where student.id=score.stu_id
group by student.id;

平均值
select c_name, avg(grade) from score group by c_name;

两张表相同的列 其中一张查询的结果为另一张表查询的条件
查询score计算机成绩小于95分同学的所有信息
select * from student where id IN (
select stu_id from score where c_name="计算机" AND grade<95);

降序排列
将计算机组成绩降序排列 不加desc是升序
select stu_id,grade from score where c_name='计算机' order by grade desc

复合多表查询
查询姓张和王同学所有信息 先从student查出学号 在通过score查出信息 并且使用%
select student.id,name,sex,birth,department,address,c_name,grade,
from student,score
where
( name LIKE '张%' or name LIKE '王%')
and
student.id=score.stu_id;
本文出自 “柯小某” 博客,请务必保留此出处http://kexl908.blog.51cto.com/605006/883831
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: