使用openGauss已經(jīng)有很長一段時間了,本文將介紹幾個簡單易用的數(shù)據(jù)庫特性。
- 單列顯示整行數(shù)據(jù)
- where比較列合并
- 獨立寫布爾列
- using關鍵字
- domain
單列顯示整行數(shù)據(jù)
首先我們準備測試數(shù)據(jù)表:
create table users(id int,name text,email text,deleted_at timestamp,delete boolean);
insert into users values(1,'jerome','chong.peng@enmotech.com',null,false);
insert into users values(2,'sara','lynn.wood@poland.com','2001-09-11',true);
insert into users values(3,'dolores','th000@sky.com',null,false);
insert into users values(4,'evan','rachel.moore@hollywood.com',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)
上面是將所有列作為行類型返回單列,可以比較簡潔的返回數(shù)據(jù)。
where比較列合并
假設我們有以下查詢:
select id, name, email
from users
where name = 'dolores'
and email = 'th000@sky.com';
根據(jù)名稱和郵箱查詢用戶,有的時候where條件后面可能會出現(xiàn)1=1
select id, name, email
from users
where 1=1
and name = 'dolores'
and email = 'th000@sky.com';
應用層需要比較方便進行where條件拼接。
其實可以去掉and,使用如下語句:
select id, name, email
from users
where (name, email) = ('dolores','th000@sky.com');
可以查詢到同樣的結(jié)果
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 = 'th000@sky.com')
or
(name = 'evan' and email = 'rachel.moore@hollywood.com')
);
可以將其縮短為:
select id, name, email
from users
where deleted_at is null
and (name, email)
in (('dolores','th000@sky.com'),('evan','rachel.moore@hollywood.com'));
這可以使查詢更短且更易于閱讀。
獨立寫布爾列
接下來的查詢,獲取未刪除的用戶,比較常見的是這種寫法:
select id, name, email
from users
where delete = false;
多數(shù)人并不知道布爾值不需要與另一個布爾值進行比較,可以這樣寫:
select id, name, email
from users
where not delete;
這樣閱讀起來也更好,結(jié)果如下:
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
using (id);
多個字段還可以使用逗號進行分隔:
on t1.a = t2.a and t1.b = t2.b
改寫為
using (a,b);
domain
domain也是比較有用的一個特性,例如可以很多需要進行相同限制的列創(chuàng)建自定義類型:
create domain my_addr varchar(100) not null default 'n/a';
或者是作為別名支持兼容性數(shù)據(jù)類型:
create domain binary_float as float;
本文總結(jié)了幾個有幫助的實用特性,大家在日常使用過程中可以進一步挖掘。
最后修改時間:2022-10-22 12:33:29
「喜歡這篇文章,您的關注和贊賞是給作者最好的鼓勵」
關注作者
【版權聲明】本文為墨天輪用戶原創(chuàng)內(nèi)容,轉(zhuǎn)載時必須標注文章的來源(墨天輪),文章鏈接,文章作者等基本信息,否則作者和墨天輪有權追究責任。如果您發(fā)現(xiàn)墨天輪中有涉嫌抄襲或者侵權的內(nèi)容,歡迎發(fā)送郵件至:contact@modb.pro進行舉報,并提供相關證據(jù),一經(jīng)查實,墨天輪將立刻刪除相關內(nèi)容。




