sql簡易基礎及使用

2021-08-28 06:12:17 字數 2067 閱讀 7114

//選擇所有列

select * from  表名;

//選擇name列

select name from 表名;

過濾重複

select distinct 列1 from 表名;

使用where後的條件需要加單引號,有的也支援雙引號,數值不需要加引號 

運算子

操作符描述=等於

<>(某些版本中寫為  != )

不等於》

大於<

小於》=

大於等於

<=

小於等於

between

範圍(後面詳細介紹)

like

某種查詢模式(後面詳細介紹)

and && or

後面會介紹

介於兩個值之間的資料範圍,不同的資料庫之間,區間範圍不同,需要自己查詢

select * from 表名 where 列1 between 資料1  and  資料2

需要查詢範圍之外的資料使用not

select * from 表名 where 列1 not between 資料1 and 資料2

注:limit字尾為整數

注:操作符

多用於where子句

select * from 表名 where 列1 in ('資料1','資料2')      (查詢表中同時存在列1和列2的資料)

注:關鍵字

模糊匹配

以man開頭的字串

select * from 表名 where 列1 like 'man%';

以man結尾的字串

select * from 表名 where 列1 like '%man';

任何位置包含man的字串

select * from 表名 where 列1 like '%man%';

以字母man結尾的四個字母

select * from 表名 where 列1 like '_man';

表示集合時,可以是集合中的任意單個字元

select * from 表名 where 列1 like '[qi]zzz[zh]n';

表示範圍時,可以是以man結尾,從[*-*]的任意單個字母

select * from 表名 where 列1 like '[c-j]man';

*表示多個字元,  ?表示單個字元,   #表示單個數字

and表示兩個條件都滿足時,查詢出的語句

or表示兩個條件只要有乙個條件滿足,查詢出的語句

兩個詞可以聯合使用

select * from 表名 where (列1='資料1' or 列2='資料2') and 列3='資料3'

排序(預設公升序) 降序需要新增 desc關鍵字,公升序為asc關鍵字

建立**

create table 表名(

列1     資料型別,

列2     資料型別,

向列中新增值

insert into 表名(列1,列2) values(資料1,資料2);

修改資料

update 表名  set 列2 = 資料2  where 列1 =資料1;(根據列1修改列2)

修改表,增刪改列

新增列alter table 表名 add column 列1 資料型別;

刪除列alter table 表名 drop column 列1 資料型別;

刪除表中的不存在的行

delete from 表名 where 列1 is null;

在不刪除表的情況下刪除所有行

delete from 表名

注:不同資料庫中top語句的使用有細微差別

sql server語法

select  top 數字 列名 from 表名 (查詢表中前數字名的列)

select  top 數字 percent 列名 from 表名  (查詢表中前百分比的列)

mysql語法

select * from 表名 limit 數字 (查詢表中數字條資料)

oracle語法

select * from 表名 where rownum<=數字 (查詢表中數字條資料)

sql使用基礎

select from tablename limit i,n tablename 表名 i 為查詢結果的索引值 預設從0開始 當i 0時可省略i n 為查詢結果返回的數量,offset i與n之間使用英文逗號 隔開 limit n 等同於 limit0,n for instance 查詢10條資料...

sql跟蹤及tkprof使用

在oracle資料庫中,awr是關於資料庫系統總體的負載情況和運 況的報告。而當系統負載都顯示正常,而client執行某些動作響應非常慢,或者某些終端連線的會話執行緩慢或異常時,就須要用到會話級別的跟蹤了。session級別跟蹤的方法有很多。比方當前會話的跟蹤,能夠執行命令 alter sessio...

sql 中case when 基礎使用

sql 語句中 case when then 簡單使用 因為表很簡單,就不為大家展示建表的 了 select from user 結果如圖 when 1 then 男 when 2 then 女 else 寵物 end 性別 from user u 查詢結果如下 從中可以看出我們的case函式是作為...