Mysql sql語句回顧1

2021-06-26 15:21:43 字數 1972 閱讀 3812

檢索出不存在相同值的列表可以加上distinct關鍵字

select distinct vend_id from products;

可以使用limit子句來限制返回結果的數量

select prod_name from products limit 5;

limit子句同樣擁有offset的功能,就是設定從第幾條資料開始獲得

select prod_name from products limit 5,5;

代表從第五個資料開始的五條資料

雖然limit具有offset的功能,但是mysql還是提供了offset子句

select prod_name from products limit 5 offset 5;

mysql的索引值是從0開始的

可以使用order by子句來給資料進行排序,該子句取乙個或多個列的名字,據此對輸出進行排序

select prod_name form products order by prod_name;

該子句可是用非檢索的列排序資料

order by子句可以選擇使用多個列排序,所選列之間使用逗號隔開。

order by 子句要仿造where子句之後

order by子句可以指定排序的方向(公升序和降序),預設是公升序,如果想變為降序需要在order by 子句的最後加上desc

desc關鍵字只應用到直接位於其前面的列名,可以使用如下的查詢方式

select prod_name,prod_price,prod_id from products order by prod_name desc,prod_name;

where 子句的操作符號中判斷相等使用=判斷不相等使用<>符號,和程式語言當中略顯不同

通過使用between關鍵字來進行範圍值檢查

select prod_name,prod_price from products where prod_price between 5 and 10;

使用is null來進行空值檢查

select prod_name from products where prod_price is null

該語句返回沒有**的產品資料

mysql中存在幾個邏輯操作符,分別是以下幾個

and or in not

select prod_id,prod_price,prod_name from products where vend_id=1003 and prod_price<=10; 取交集

select prod_name,prod_price from products where vend_id=1002 or vend_id=1003;取並集

select prod_name,prod_price from products where vend_id in(1002,1003) order by prod_name; 取交集

select prod_name,prod_price from products where vend_id not in(1002,1003) order by prod_name; 取並集的補集

可以使用圓括號來標記計算次序

%萬用字元表示任何字元出現任意次序,使用like操作符

select prod_id,prod_name from products where prod_name like 『jet%』;

該查詢語句會查詢所有prod_name欄位以jet開頭的行

_萬用字元匹配單個任意字元

select prod_id,prod_name from products where prod_name like 『_ ton anvil』;

該查詢語句查詢任何以ton anvil 為結尾並且在其之前只有乙個字元的所有行

不要過度使用萬用字元,如果其他操作能達到相同的目的,應該使用其他操作符。

在確實需要萬用字元時,除非絕對有必要,否則不要把它們用在搜尋模式的開始處,這樣最慢。

MySql Sql語句總結

建表語句 create table class id int primary key,class char 255 name varchar 4000 hobby text int 和 integer 是一樣的,只是為了簡寫罷了,主鍵宣告直接跟在定義後面,char和varchar char是固定長度...

MySQL SQL語句優化

檢視表定義 show create table users 檢視表的索引 show index from users 你要獲取第乙個表的所有資訊,你說全表掃瞄快呢還是索引掃瞄快呢?所以當你查詢庫 包括left join中的臨時庫 的所有資訊時,資料庫會選擇最優方法 全表掃瞄!s表dept id na...

MySQL sql語句筆記

sql 插入insert into 表名 列名 values 值列表 關鍵字 可省略 insert into stu name,age,city values 18,1 更新 update 表名 set 列名 更新值 where 更新條件 update stu set name sdfsdf age...