mysql 公式 MySQL常見公式彙總1

2021-10-17 14:02:21 字數 4101 閱讀 1642

#篩選每組比自己小的數量個數

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常見用法

檢視慢日誌 show variables like slow query log show variables like long query time 設定慢日誌記錄什麼樣的sql,預設10s log queries not using indexes 未使用索引的查詢也被記錄到慢查詢日誌中,一般...

Mysql常用公式彙總

基本的增刪查改 向stu表中新增資料 1 insert into stu values 內容,內容,內容 2 insert into stu 欄位名,欄位名,欄位名 vaules 內容,內容,內容 3 insert into stu 欄位名,欄位名,欄位名 values 內容,內容,內容 內容,內容...

mysql設 mysql常見設定

1.如果你需要設定mysql的埠,預設字符集的話,除了在圖形介面下設定之外,還可以去配置檔案那裡設定 例如 mysql在c program files mysql mysql server 5.0目錄下,則尋找my.ini檔案,裡面可以設定 埠號 port 和預設字符集 default charac...