查询数据范例:
 
  1. create table student ( 
  2.   id INT(10) NOT NULL UNIQUE PRIMARY  KEY
  3.   name VARCHAR(20) NOT NULL
  4.   sex VARCHAR(4), 
  5.   birth YEAR
  6.   department VARCHAR(20), 
  7.   address VARCHAR(50) 
  8.   ); 
  9.    
  10.    
  11. create table score ( 
  12.   id INT(10) NOT NULL UNIQUE PRIMARY KEY  AUTO_INCREMENT, 
  13.   stu_id INT(10) NOT NULL
  14.   c_name VARCHAR(20), 
  15.   grade INT(10) 
  16.   ); 
  17.    
  18. insert into student  VALUES (901,'张老大','男','1985','计算机系','北京市海淀区'); 
  19. insert into student  VALUES (902,'张老二','男','1986','中文系','北京市昌平区'); 
  20. insert into student  VALUES (903,'张三','女','1990','中文系','湖南省永州市'); 
  21. insert into student  VALUES (904,'李四','男','1990','英语系','辽宁省阜新市'); 
  22. insert into student  VALUES (905,'王五','女','1991','英语系','福建省厦门市'); 
  23. insert into student  VALUES (906,'王六','男','1988','计算机系','湖南省衡阳市');   
  24.    
  25. insert into  score   VALUES  (NULL,901,'计算机','98'); 
  26. insert into  score   VALUES  (NULL,901,'英语','80'); 
  27. insert into  score   VALUES  (NULL,902,'计算机','65'); 
  28. insert into  score   VALUES  (NULL,902,'中文','88'); 
  29. insert into  score   VALUES  (NULL,903,'中文','85'); 
  30. insert into  score   VALUES  (NULL,903,'计算机','98'); 
  31. insert into  score   VALUES  (NULL,904,'计算机','70'); 
  32. insert into  score   VALUES  (NULL,904,'英语','92'); 
  33. insert into  score   VALUES  (NULL,905,'英语','94'); 
  34. insert into  score   VALUES  (NULL,906,'计算机','90'); 
  35. insert into  score   VALUES  (NULL,906,'英语','85'); 
 
 
列表查询
  1. select * from student  where  department IN  ('计算机系','英语系'); 
   
按条件查询
  1. select * from student  where  department='计算机系'  or  department='英语系'
 
查询范围
 
  1. select * from student where 2009-birth between 18 AND 22; 
  2. select * FROM student where 2009-birth>=18 AND 2009-birth<=22; 
 
分组查询 group by 
 
  1. select department, COUNT(id) from  student  group by  department;  
  2. select department, COUNT(id) AS sum_info  from  student  group by  department; 
  3.  
  4. 说明一:如果在一个表里N个用户每个用户有多个收入,需要计算group by 
  5. select name,SUM(num) from test group by name
  6. 说明二:计算每个用户信息出现的次数 
  7. select name,count(num) from test group by name 
 
 
分组查询最大值 在所有的c_name中,grade列中最大的一个,即使一个c_name有多个值
  1. 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;