一些常用的sql语句小结

上一篇 / 下一篇  2013-04-26 09:16:26 / 个人分类:数据库相关

1、DML
:插入数据的语句
  insert into 表名 values(值,值,值,...);   注意:插入的数据和列要一一对应
  insert into 表名 values(..),(..),..;         同时插入多条语句
  insert into 表名(列名1,列名2,...) values (值1,值2,...);

:删除数据的语句
  delete form. 表名;                            删除表中的所有记录
  delete from 表名 where  id=3;                删除一条记录

:修改数据的语句
  update 表名 set name='王五',age='22' where id='1005';

:多种查询语句
   单个条件的查询
     select * from 表名 where age>23;
   多条件查询(查找年龄小于30大于23的)
     select * from 表名 where age between 23 and 30;
     select * from 表名 where age int(23,30);
     select * from 表名 where age>23 and age<30;
     select * from 表名 where department='测试部' and(or) age>22;
  查看表的前三条记录
     select * from 表名 limit 0,3;
     select * from 表名 limit 3,6;             查从第4条开始,查6条记录;
  排序查看
     select * from 表名 order by age(asc);  以升序的排列查看表的信息
     select * from 表名 order by age desc;  降序查看
     select * from 表名 where age>23 order by desc;  查看23岁以上的降序排列
  结果计算查看
     select count(*) from 表名 where department='测试部';
  考试成绩单的分析
    1、在表中按sid分组,并求每组平均值
    select avg(g) from 表名 group by sid;    
 
    2、在表中按sid分组,求出每组大于70分的成绩的平均分
    select sid,avg(g) from 表名 where g>70 group by sid; 
            
    3、只显示平均值大于85分的成绩
    select sid,avg(g) form. 表名 group by sid having avg(g)>85;

    4、首先去掉g<70分的,然后再求出平均值,然后只显示平均值大于84分的
    select sid,avg(g) from 表名 where g>70 group by sid having avg(g)>84 order by avg(g);
    
    


TAG:

helianthus的个人空间 引用 删除 helianthus   /   2013-08-07 10:14:32
博主赔我眼睛,要瞅瞎啦。。。 博主的此文可以再不断充实下,总结的很实用。
 

评分:0

我来说两句

Open Toolbar