一些常用的sql查询语句

1、模糊查询字段A中含有字符串“about”的

select 字段A from 数据表名 where 字段A like '%about%'

2、替换数据库里面的某个字段为某个值

update yhhaoren set za = replace(za,'0','1')

意思是将yhhaoren数据表中的za字段里面的字符0替换为字符1

3、将字段A的内容复制到字段B

update '数据表' set 字段B =字段A;

4、删除ID是在某个范围内的数据

delete from 表名称 where id>=42 and id<=3000

5、统一给一个字段赋值

update 表名称 set 字段名 = '100'

6、清空表,并且自增id从头开始

truncate table 表名

7、统计表里面有多少条数据

select count(*) from 表名称 where 字段名='条件'

如果不需要条件限制,就把where后面去掉

8、查询表里某个字段不为空的总数

select count(*) from 表名 where 字段名 is not null

9、查询出表里某个字段的所有重复值

select * from 数据表 where 字段 in (
	select 字段 from 数据表 where LENGTH(字段)>0 GROUP BY 字段 HAVING count(字段)>1 )
	order by 字段

10、查询某个字段包含的内容批量替换

update 数据表名 set 字段名=replace(字段名,'要替换的内容','替换后的内容')

11、查询两张表某个字段值相同的数据

select * from A,B where A.username = B.username

A、B代表表名字,username代表字段名,整句sql表示,查询A,B两张表中username字段值相同的数据。

12、查询两表并更新某个字段

update A,B set A.cs=101 WHERE A.id=B.id;

意思是查询A,B两个表中id相同的内容,并更新A表中的字段cs内容为101。

update A,B set A.cs=replace(A.cs,'101','105')  WHERE A.id=B.id;

意思是查询A,B两个表中id相同的内容,并把A表中字段cs的内容为101的数据统一更新为内容105。

update A, B set A.title=B.title where A.id=B.id;

意思是查询A,B两个表中id相同的内容,并把A表中字段title的内容更新为B表的title内容。
13、查询日期

1select * from TABLE where regtime < '2021-08-01'; //直接日期
2select * from TABLE where regtime < 1609398384; //时间戳
3select * from TABLE where FROM_UNIXTIME(regtime)<='2020-12-31 00:00:00' //转换时间戳
4select * from TABLE where FROM_UNIXTIME(regtime)<='2020-12-31 00:00:00' and FROM_UNIXTIME(regtime)>='2020-01-01 00:00:00'; //时间区间

猜你喜欢

转载自blog.csdn.net/likeni1314/article/details/88780270