经常用到的sql语句

获取一个表中的数据插入到另一个表

insert into studentsNew(id,name,sex,age) select id,name,sex,age from students where id between 30000 and 100000;

设置某列的默认值

alter table studentsNew alter column begindate set default 20171222;
alter table studentsNew alter column enddate set default 99991231;

更新原表关联数据,插入新增数据(新表中少两列字段)

UPDATE students tt set enddate=20171223 where exists(select * from stu ss where tt.id=ss.id);
alter table students alter column begindate set default 20171223;
alter table students alter column enddate set default 99991231;
insert into students(id,name,sex,age) select id,name,sex,age from stu;
select * from students st, stu ss
where st.id=ss.id

删除表1中两个表相同数据,再把表2数据全部插入到表1

DELETE from students tt WHERE exists(select * from stu ss where tt.id=ss.id);
insert into students(id,name,sex,age) select id,name,sex,age from stu;

查询30000-100000中间数据

select * from students where id>30000 and id<100000
select * from students where id between 30000 and 100000

获取一个表中的数据插入到另一个表

insert into studentsNew(id,name,sex,age) select id,name,sex,age from students where id between 30000 and 100000;

设置某列的默认值

alter table studentsNew alter column begindate set default 20171222;
alter table studentsNew alter column enddate set default 99991231;

查询两表的关联数据

select * from studentsNew ss,stu_test tt where ss.id=tt.id

更新表一列的数据

update stu_test set begindate=20171223

    原文作者:曹小琳琳
    原文地址: https://www.jianshu.com/p/71bb91099c59
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞