MySQL資料庫全面單錶查詢方法

2021-10-09 12:35:31 字數 2492 閱讀 8942

當前使用中的資料庫

select database();

檢視當前mysql版本

select version();

檢視當前使用者

select user();

檢視運算結果

select 1+2;

//欄位用','隔開,至少有乙個字段,最終結果集按照這個順序顯示

select 欄位1,欄位2... from 表名;

//*代表所有的字段

select *from 表名;

//去重後的結果,distinct必須緊接著select後面

select distinct 字段 from 表名;

//統計不重複的個數

select count (distinct 字段) from 表名;

where子句適用於對記錄的刪 、 改、 查的操作

對記錄進行過濾,如果沒有指定的where子句時,則會顯示所有的記錄

在where表示式中,可以使用函式或者運算子,運算子包含:

型別運算子

算術+ - * / %

比較》 < >= <= != =

邏輯or||and&¬!

提公升優先順序

()

例如:
where 條件關鍵字

in: 查詢乙個集合的資料

//查詢id是(5,6,9)的資料

select * from students where id=5 || id=6 || id=9;

select * from students where id in(5,6,9);

//一次刪除多個資料

delete from students where id=5 || id=6 || id=9;

delete from students where id in(5,6,9);

between *** and ***:查詢乙個區間的資料

//查詢id在1-10之間的資料

select * from students where id>1 && id<10;

select * from students where id between 1 and 10;

//刪除id在1-10之間的資料

delete from students where id between 1 and 10;

not:排除

用於模糊查詢(符號%;)任意長度,(符號_;)乙個字元的長度

//查詢name名字以張開頭的資料

select * from students where name like '張%'

//查詢name名字包含字母三的資料

select * from students where name like '%三%'

//搜尋name以華結尾的兩位數資料

select * from students where name like '_5';

控制查詢記錄的條數,索引從0開始

select from students limit 2 

select * from students limit 3,4 //從索引為3的記錄開始,返回4條記錄

//php中的分頁功能,偏移值的計算,(當前頁-1)*每頁記錄數

select name from student limit 4 offset 3

//還可以使用offset(偏移):從索引為3的記錄開始,返回4條

根據給定的資料表的每列的各個成對查詢結果進行分組統計,最終得到乙個分組的彙總記錄表,利用group by分組資訊進行統計,常見的是配合max等聚合函式篩選資料化分析。

select指定的字段要麼作為分組的依據(group by語句的後面),要麼就要被包含在聚合函式中。

//簡單分組查詢

select *** from students group by ***;

//聚合函式分組

select count(*),city from students group by city;

按照給定的字段進行排序,asc:公升序(預設) desc:降序

如果同時選擇多個字段,先按第乙個字段排序,如果第乙個字段值相等,再嘗試第二個字段,以此類推

//預設公升序

select * from students order by birthday asc(可寫可不寫);

//降序

select * from students order by birthday desc;

select-----欄位----from----表名----where----group by----order by----limit

mysql資料庫筆記(三) 查詢資料 單錶查詢

查詢語法 語法格式 select column name,column name from table name 查詢emp表中資訊 查詢單個列 查詢員工的員工編號號資訊 select id from s emp 查詢多個列 查詢員工的員工編號,姓名和工資資訊 select id,last name...

mysql查詢資料庫表

使用sql語句查詢mysql指定表字段。可以方便整理資料庫表的說明文件。我在工作中整理了部分sql作為記錄。可以用在以後的mysql文件匯出工具裡。以下為具體內容 使用sql查詢指定資料庫表名和表說明。select table name as tablename,table comment as c...

MySQL資料庫,相關子查詢,單錶查詢和排序

sql select 語句 select 語句用於從表中選取資料。結果被儲存在乙個結果表中 稱為結果集 sql 中最重要的 ddl 語句 create database 建立新資料庫 alter database 修改資料庫 create table 建立新錶 alter table 變更 改變 資...