Some commonly used sql query statements

1. Fuzzy query field A contains the string "about"

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

2. Replace a certain field in the database with a certain value

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

It means replacing the character 0 in the za field in the yhhaoren data table with the character 1

3. Copy the contents of field A to field B

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

4. Delete data whose ID is within a certain range

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

5. Assign a value to a field uniformly

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

6. Clear the table and start from scratch by incrementing the ID

truncate table 表名

7. How many pieces of data are there in the statistical table?

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

If there is no need for conditional restrictions, just remove where after

8. Query the total number of fields in the table that are not empty

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

9. Query all duplicate values ​​of a field in the table

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

10. Query the content contained in a certain field and replace it in batches

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

11. Query data with the same field value in two tables

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

A and B represent the table names, username represents the field name, and the entire SQL statement means querying data with the same username field value in the two tables A and B.

12. Query two tables and update a certain field

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

It means to query the content with the same ID in two tables A and B, and update the content of field cs in table A to 101.

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

It means to query the content with the same ID in the two tables A and B, and update the data with the content 101 of the field cs in the A table to the content 105.

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

It means to query the content with the same ID in two tables A and B, and update the content of the field title in table A to the title content of table B.
13. Inquiry date

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'; //时间区间

Guess you like

Origin blog.csdn.net/likeni1314/article/details/88780270