mysql入门学习之一:查询
2012-05-31 16:13
441 查看
查询数据范例:
列表查询
按条件查询
查询范围
分组查询 group by
分组查询最大值 在所有的c_name中,grade列中最大的一个,即使一个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
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
相关文章推荐
- php入门学习知识点一 PHP与MYSql连接与查询
- [入门]MySQL学习笔记三数据查询筛选显示
- php入门学习知识点一 PHP与MYSql连接与查询
- mySQL学习入门教程——2.创建表
- mysql学习记录:单表查询(补充1)
- mysql中的联合查询union和union all 学习记录。
- MySQL入门学习(一)安装篇
- MySQL 学习<六> 查询语句
- MyBatis入门学习教程-实现关联表查询
- MYSQL 入门操作学习指南(建库,建表 增删改查 、表结构修改、函数、字符集校对等)
- MySQL学习之聚合函数查询
- MySQL入门学习(一)
- MySQL入门学习(三)建库建表篇
- mysql学习- 将查询结果加上其他键值插入到表中
- Mysql学习总结(12)——21分钟Mysql入门教程
- 从项目上一个子查询扩展学习开来:mysql的查询、子查询及连接查询
- Mysql入门学习笔记---基本操作
- MySQL入门学习(三)
- MYSQL入门学习之三:全文本搜索
- MYSQL入门学习之十二:存储过程的基本操作