常用sql整理

查所有数据

select * from table;

查询带条件

select * from table where 1=1;

查询分组

select field from table group by field;

查询嵌套子查询

select * from (select * from table where 1=1) as t;

两张表联查

select * from table1 as t1 (left/right/full outer) join table2 as t2 on t1.field1 = t2.field1

三表及以上联查

select * from table1 as t1,table2 as t2,table3 as t3 where t1.field1 = t2.field1 and t2.field1 = t3.field1

分页查询

select * from testtable limit 20 offset 0;     // 从第0开始,查20条
select * from testtable limit 20 offset 40;    // 从第40开始,查20条

模糊查询

select * from table where field like '%val1%'

查超过一天前的数据

select * from table where date(now()) - FROM_UNIXTIME(time_field, '%Y%m%d') > 0;

添加(字符串需加" ")

insert into table(field1,field2) values(val1,val2);

修改所有数据

update table set field1=val1,field2=val2;

修改一条数据(字符串需加" ")

update table set field1=val1,field2=val2 where field=val3;

批量修改数据(字符串需加" ")

update table set field1=val1 where field2 in (val2,val3,val4)

删除所有数据

delete from table

删除一条数据(字符串需加" ")

delete from table where field=val1

 

posted @ 2020-09-18 09:36  张永峰z  阅读(155)  评论(0编辑  收藏  举报