openGauss易知易會的幾個實用特性
本文將介紹幾個簡單易用的資料庫特性。
- 單列顯示整行資料
- where比較列合併
- 獨立寫布林列
- using關鍵字
- domain
單列顯示整行資料
首先我們準備測試資料表:
create table users(id int,name text,email text,deleted_at timestamp,delete boolean);
insert into users values(1,'jerome','[email protected]',null,false);
insert into users values(2,'sara','[email protected]','2001-09-11',true);
insert into users values(3,'dolores','[email protected]',null,false);
insert into users values(4,'evan','[email protected]',null,false);
通常我們使用如下語句進行查詢
openGauss# select * from users;
id | name | email | deleted_at | delete
----+---------+----------------------------+---------------------+--------
1 | jerome | chong.peng@enmotech.com | | f
2 | sara | lynn.wood@poland.com | 2001-09-11 00:00:00 | t
3 | dolores | th000@sky.com | | f
4 | evan | rachel.moore@hollywood.com | | f
(4 rows)
也可以使用下面的語句進行查詢,尤其是列較多時
openGauss# select users from users;
users
-------------------------------------------------------
(1,jerome,chong.peng@enmotech.com,,f)
(2,sara,lynn.wood@poland.com,"2001-09-11 00:00:00",t)
(3,dolores,th000@sky.com,,f)
(4,evan,rachel.moore@hollywood.com,,f)
(4 rows)
上面是將所有列作為行型別返回單列,可以比較簡潔的返回資料。
where比較列合併
假設我們有以下查詢:
select id, name, email
from users
where name = 'dolores'
and email = '[email protected]';
根據名稱和郵箱查詢使用者,有的時候where條件後面可能會出現1=1
select id, name, email
from users
where 1=1
and name = 'dolores'
and email = '[email protected]';
應用層需要比較方便進行where條件拼接。
其實可以去掉and,使用如下語句:
select id, name, email
from users
where (name, email) = ('dolores','[email protected]');
可以查詢到同樣的結果
id | name | email
----+---------+---------------
3 | dolores | th000@sky.com
(1 row)
我們還可以使用in來滿足or條件,例如下面的查詢:
select id, name, email
from users
where deleted_at is null
and (
(name = 'dolores' and email = '[email protected]')
or
(name = 'evan' and email = '[email protected]')
);
可以將其縮短為:
select id, name, email
from users
where deleted_at is null
and (name, email)
in (('dolores','[email protected]'),('evan','[email protected]'));
這可以使查詢更短且更易於閱讀。
獨立寫布林列
接下來的查詢,獲取未刪除的使用者,比較常見的是這種寫法:
select id, name, email
from users
where delete = false;
多數人並不知道布林值不需要與另一個布林值進行比較,可以這樣寫:
select id, name, email
from users
where not delete;select id, name, email
from users
where not delete;
這樣閱讀起來也更好,結果如下:
id | name | email
----+---------+----------------------------
1 | jerome | chong.peng@enmotech.com
3 | dolores | th000@sky.com
4 | evan | rachel.moore@hollywood.com
(3 rows)
using關鍵字
當我們做多張表的join連線時,如果join欄位的名稱相同可以使用using關鍵字來簡化語句
select ...
from t1
join t2
on t1.id = t2.id;select ...
from t1
join t2
on t1.id = t2.id;
可以改寫為:
select …
from t1
join t2
using (id);
多個欄位還可以使用逗號進行分隔:
on t1.a = t2.a and t1.b = t2.b
改寫為
using (a,b);
domain
domain也是比較有用的一個特性,例如可以很多需要進行相同限制的列建立自定義型別:
create domain my_addr varchar(100) not null default 'n/a';create domain my_addr varchar(100) not null default 'n/a';
或者是作為別名支援相容性資料型別:
create domain binary_float as float;
本文總結了幾個有幫助的實用特性,大家在日常使用過程中可以進一步挖掘。
「其他文章」
- GaussDB資料型別轉換介紹
- 通過公網連線GaussDB資料庫例項
- GaussDB資料型別介紹
- 如何通過DAS連線GaussDB
- 企業級分散式資料庫 - GaussDB介紹
- GaussDB 資料庫實驗環境搭建指導
- Tableau連線openGauss實踐
- 以學校資料模型為例,掌握在DAS下使用GaussDB
- openGauss資料庫共享儲存特性簡介
- openGauss資料庫原始碼解析系列文章——備份恢復機制:openGauss增量備份技術(上)
- openGauss資料庫客戶端接入認證詳解
- Excel連線openGauss資料庫實操
- openGauss資料庫原始碼解析系列文章——備份恢復機制:openGauss全量備份技術
- 超市進銷存之openGauss資料庫的應用與實踐
- 在WPS表格裡製作連線到openGauss的實時重新整理報表
- openGauss資料庫PostGIS 安裝與使用
- openGauss中Schema賦權小試
- openGauss Cluster Manager RTO Test
- 【我和openGauss的故事】openGauss獲獎專案講解
- openGauss易知易會的幾個實用特性