SQL 基礎

http://www.postgresqltutorial.com/

不選空值 NOT NULL AND EMPTY

SELECT device_token FROM users WHERE device_token IS NOT NULL AND device_token != '';

選不重複

SELECT DISTINCT ON device_token FROM users;

Data type

https://www.w3schools.com/sql/sql_datatypes.asp

設定column預設值

ALTER TABLE <Table名稱> ALTER COLUMN <column名稱> SET DEFAULT <預設值>;

或是建表時設定

使用變數

可以使用 with as,後面一定要接著 select 語句

地理位置距離計算

也可參考 postGIS https://postgis.net/

https://stackoverflow.com/a/25140461

記得使用 point 類型

搜尋兩人距離範例:

計算與每個人的經緯度的距離

完整範例:

DISTINCT 與 ORDER BY 一起使用

DISTINCT ON 只有 postgres 有, MySQL 要使用 group by,另外 postgres 的 group by 不能單獨選擇特定對象,必須要包含在 select 裡面

https://stackoverflow.com/a/9796104/4622645

DISTINCT ON 並選擇要從重複的選擇哪些

如果 A 有多筆資料,想要 DISTINCT A 中最新的,之後把選出來的 A, B, C 再依照時間排序

使用 TRANSACTION

https://stackoverflow.com/questions/43351168/node-postgres-transactions-with-callbacks-or-async-await

Last updated

Was this helpful?