mysql表的增删改select 和 where

2023-03-26 20:35:42

创建表

create table t1(
    id int primary key auto_increment,
    username char(12) not null,
    sex enum('male','female') default 'male',
    hobby set('study','sing','eat','drink')
);


create table t2(id int,name char(12));

insert into 表(字段,.......) values(值,........);

insert into t1 values(1,'mike','male','sing,drink'),
(2,'ali','female','sing'),
(3,'amy','female','sing,drink,study'),
(4,'Gsir','male','drink');


insert into t1(username,hobby) values('zhangsan','study');


insert into t2(id,name) select id,username from t1;

 删

mysql> delete from t2;  (如果有自增id  不会清除自增字段的offset值)

# show create table t1;               查看t1目前偏移量

 mysql> delete from t1 where id = 3;

mysql> truncate table t1;

        # 会清空表和自增字段的偏移量


 改

update 表 set 字段=值 where 条件;

update 表 set 字段=值,字段=值 where 条件;

mysql> update t1 set id = 5,hobby = 'sing' where id  = 4;

select

select * from 表;

select 字段... form 表;

# 重命名

select 字段 as 新名字,... form 表;

select 字段 新名字,... form 表;

# 避免重复

select distinct post from  employee;

# 四则运算

mysql> select emp_name,salary*12 as annual_salary from employee;

# concat  拼接函数

mysql> select concat('a','b','c');

# 单表查询
create table employee(
id int not null unique auto_increment,
emp_name varchar(20) not null,
sex enum('male','female') not null default 'male', #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一个部门一个屋子
depart_id int
);


insert into employee(emp_name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'20170301','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部
('alex','male',78,'20150302','teacher',1000000.31,401,1),
('wupeiqi','male',81,'20130305','teacher',8300,401,1),
('yuanhao','male',73,'20140701','teacher',3500,401,1),
('liwenzhou','male',28,'20121101','teacher',2100,401,1),
('jingliyang','female',18,'20110211','teacher',9000,401,1),
('jinxin','male',18,'19000301','teacher',30000,401,1),
('成龙','male',48,'20101111','teacher',10000,401,1),

('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),

('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬银','female',18,'20130311','operation',19000,403,3),
('程咬铜','male',18,'20150411','operation',18000,403,3),
('程咬铁','female',18,'20140512','operation',17000,403,3)
;


mysql> select id,emp_name from employee;


mysql> select id,emp_name as name from employee;

mysql> select id,emp_name as name from employee;

# 避免重复

mysql> select distinct post from employee;

# 四则运算

mysql> select emp_name,salary*12 as annual_salary from employee;

# concat

 mysql> select concat(emp_name,':',salary)from employee;

  mysql> select concat_ws('|',emp_name,salary*12) as annual_salary from employee;

where

mysql> select * from employee where post != 'sale';

mysql> select * from employee where id < 10;

between...and 

mysql> select * from employee where salary between 10000 and 14000;

 

in

mysql> select * from employee where salary in (10000,18000,200000);

模糊匹配

 like

        # % 通配符 任意长度的任意内容

                mysql> select * from employee where emp_name like 'j%';

                mysql> select * from employee where emp_name like '%j%';

                mysql> select * from employee where emp_name like '%j';

        # _ 通配符  一个长度的一个内容

                mysql> select * from employee where emp_name like '%e_';

 

                

 regexp

mysql> select * from employee where emp_name regexp '^j';

 mysql> select * from employee where salary regexp '\d{4,5}';

not in     and   or

 select * from employee where salary not in (10000,20000) and age = 18;

mysql> select * from employee where sex = 'male' and post = 'sale' or id < 5;

 

 

  • 作者:凤栖梧桐123
  • 原文链接:https://blog.csdn.net/weixin_46673625/article/details/124359060
    更新时间:2023-03-26 20:35:42