Mysql常用公式汇总

基本的增删查改
#向stu表中添加数据
1、insert into stu values (内容,内容,内容);
2、insert into stu(字段名,字段名,字段名) values(内容,内容,内容);
3、insert into stu(字段名,字段名,字段名) values(内容,内容,内容),
                                                                            (内容,内容,内容),
                        (内容,内容,内容);
#查询、修改表单内数据
1、查看到stu表
select*from stu;
2、修改表单内数据
update 表名 set 字段计算1,字段计算2... where 条件;

#删除表单内数据
delete from stu where 唯一ID=内容;
#筛选每组比自己小的数量个数

select 
t1.company,
t1.remark,
t1.score,
(select count(1) from sheet t2 where t1.remark=t2.remark and t2.score
from sheet t1;
 
#分组统计比自身小的个数
select 
a1.ipc,
a1.name,
a1.cishu,
(((select count(1) from a a2 where a1.ipc=a2.ipc and a2.cishu
(select count(1) from a a2 where a1.ipc=a2.ipc and a2.cishu=a1.cishu)/2)/(select count(cishu) from a a2 where a1.ipc=a2.ipc)) count
from a a1;
 
#筛选前10条数据
select * from table_name limit 0,10 
 
 
#从左边截取三个字符
create table bbb1
select inventor,ipc,jishu,left(ipc,3) as ipc1 from bbb
 
 
#合并同一字段的另一列数据,并存放在另一个表中
create table mygoods1
select cat_id,group_concat(distinct price separator ';') from mygoods group by cat_id;
 
#去空格,替换
update bbb set ipc=replace(ipc,' ','')
 
#分组统计
create table `高校IPC大组统计`
select applicants,ipc_MainGroup,count(ipc_MainGroup) from `统计结果2` group by applicants,ipc_MainGroup order by applicants
 
 
#删除score列字段为空的记录
delete from tb where `SCORE` is NULL
 
#删除score为90的记录
delete from tb where `SCORE` = 90
 
#方法一、去重 对name和type相同的数值去重,并筛选出id最小的一列
select id,name,type from sheet1
where id in(select min(id) from sheet1 group by name);
 
#创建一个临时表进行判断
create table sheet3
select *,case type when '大型企业' then '4' when '中型企业' then '3'
when '中小型企业' then '2' when '小微型企业' then '1' else '0' end as type1 from sheet1;
#新建一个表,存放结果
create table sheet6
select name,type,source,max(type1) from sheet3 group by name;
 
 
#用max选择最大的,建一个虚拟表sheet4,然后再left outer join关联其他字段,并将查询的结果存放到sheet5中
create table sheet5
select sheet4.*,sheet3.type from 
(select name,max(type1) as type2 from sheet3 group by name) as sheet4 left outer join 
sheet3 on sheet4.name=sheet3.name and sheet4.type2=sheet3.type1;
 
#查询每组的前n条记录,此处为前三条记录,修改数字3即可筛选另外前N条记录
#查询每个分类中价格最高的两个商品
SELECT mygoods1.* FROM mygoods mygoods1 WHERE 
(SELECT count(*) FROM mygoods WHERE cat_id=mygoods1.cat_id AND price> mygoods1.price)< 3 
ORDER BY mygoods1.cat_id,mygoods1.price DESC;
 
#合并时去重,并用分号隔开(默认隔开符号为逗号)
select cat_id,group_concat(distinct price separator ';') from mygoods group by cat_id;
 
 
#以cat_id分组,把price字段的值打印在一行,逗号分隔,按照price倒序排列
select cat_id,group_concat(price order by price desc) from mygoods group by cat_id;
 
#批量加数字,2列合并成一列
update id17 set inventor = concat(id,inventor)
 
#批量逗号转化为id+逗号
update id17 set inventor = replace(inventor,',',CONCAT(',',id)) 
 
查找出表mygoods的goods_id不在mygoods2中的记录
select * from mygoods where goods_id not in (select goods_id from mygoods2);
 
#查询usedname这一列不为空的记录
#方法一
select * from companyname where usedname is not null;
 
#方法二
select * from table where usedname <> "";
select * from table where usedname != "";
 
#查询为空的记录
select * from table where id ="";
select * from table where isNull(id);
#具体情况具体分析,如果字段是char或者varchar类型的,使用id=""可以的;
#如果字段是int类型的,使用isNull会好些。
 
#查询表
SELECT * FROM `企业指标_标准化名称_20190703` WHERE tech_name is not null  limit 10
 
#去除空格
update 表 set 列名 =replace(列名,' ','')
 
#去除name字段末尾的逗号
update 表名 set name=left(name,char_length(name)-1) where right(name,1)=','
 
#null值替换为0
select IFNULL(列名,0) from 表名
 
#空字符串替换为0
select IF(数据2,数据2,0) from test
 
#MySQL随机取一条记录(并列随机取1个)
select a.* from 表名 a where code = (select code from 表名 where count = a.count limit 1) order by a.count
 
#按name分组取val最大的值所在行的数据
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
 
#按name分组取val最大的值所在行的数据
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
 
#按name分组取第一次出现的行所在的数据 
select a.* from tb a where val = (select val from tb where name = a.name limit 1) order by a.name
posted @ 2022-02-08 14:38  苏仙学长  阅读(374)  评论(0编辑  收藏  举报