Example table:
Instantly update all records in a column
update 表名 set 列名=设置的值
Example: Add a column of data type remark, and update all the values of the remark column to "no special record"
alter table tb add remark varchar(100);
update tb set remark='无特殊记录';
Note: You can add the --safe--updates option when starting the MYSQL monitor. If there is no where condition on the column, update or delete cannot be executed
Modify only eligible records:
update 表名 set 列名=设置的值 where 条件;
Example: Change the remark of the column sales greater than or equal to 100 to "excellent"
update tb set remark='优秀' where sales>=100;
Example: Modify the remarks of the 3 records with the lowest sales to "Come on"
Idea: use order by to arrange the column sales in ascending order, and use limit3 to select the first 3 records, and then enter "come on" into the column remark
update tb set remark='加油'
order by sales
limit 3;
Copy eligible records:
Copy only specified records: Copy eligible records to other tables
create table 新表名 select * from where
Example: Copy the column structure of table tb and the record with id A101, and then create a new table tb_A101
create table tb_A101
select * from tb
where id like 'A101';
Example: Insert the above record into an existing table
insert into 已存在的表 select * from tb where id like 'A101';
Example: Copy the records from the 2nd to the 5th in tb according to the sales to the new table
Idea: When executing create table ... select ..., use order by to sort, and then use Limit and offset to specify the number of records to be copied and the position to start copying
create table tb_2to5
select *
from tb
order by sales desc
limit 4 offset1;
Delete eligible records:
Delete all records:
delete from 表名;
The delete command deletes records, but does not delete the column structure of the table. The drop table command is required to drop the table itself
Delete the specified record:
delete from 表名 where 条件;
Example: Delete employee records younger than 30 years old
delete from tb where age<30;
Delete after sorting:
Example: delete the top 4 records with sales
delete from tb
order by sales desc
limit 4;