Mysql常用公式彙總

2022-09-20 11:54:15 字數 4374 閱讀 8762

基本的增刪查改

#向stu表中新增資料

1、insert into stu values (內容,內容,內容);

2、insert into stu(欄位名,欄位名,欄位名) vaules(內容,內容,內容);

3、insert into stu(欄位名,欄位名,欄位名) values(內容,內容,內容),

(內容,內容,內容),

(內容,內容,內容);

#查詢、修改表單內資料

1、檢視到stu表

selecct*from stu;

2、修改表單內資料

update stu 字段= '內容' , 字段 = '內容' where 唯一id = 內容;

#刪除表單內資料

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大組統計`

#刪除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

mysql 公式 MySQL常見公式彙總1

篩選每組比自己小的數量個數 select t1.company,t1.remark,t1.score,select count 1 from sheet t2 where t1.remark t2.remark and t2.score from sheet t1 分組統計比自身小的個數 selec...

excel公式彙總

目錄 1.條件判斷 2.擷取字串 midsumif sumproduct 3.排名 rank函式排名 sumproduct sumproduct函式排名 4.excel同一單元格內的數字各個位數累加求和 5.excel 判斷字元最後乙個字,並按條件處理 if函式和and or函式的組合多條件判斷技巧...

mysql常用操作彙總

本文總結了mysql的常用命令,系統管理 連線mysql 格式 mysql h 主機位址 u使用者名稱 p使用者密碼 例 1 連線到本機上的 mysql。hadoop ubuntu mysql uroot pmysql 例 2 連線到遠端主機上的 mysql。hadoop ubuntu mysql ...