如何告诉 PostgreSQL 返回第一个遇到的值而不是聚合列?
Table "public.cache"
Column | Type | Modifiers | Storage | Stats target | Description
-----------+---------+-----------+---------+--------------+-------------
user_id | integer | | plain | |
object_id | integer | | plain | |
data | integer | | plain | |
data2 | boolean | | plain | |
Indexes:
"cache_object_id_user_id_key" UNIQUE CONSTRAINT, btree (object_id, user_id)
"cache_user_id_object_id_key" UNIQUE CONSTRAINT, btree (user_id, object_id)
Has OIDs: no
Run Code Online (Sandbox Code Playgroud)
按 object_id 和 data2 进行查询分组将使哈希聚合,这是我想避免的。
SELECT object_id, data2 FROM cache GROUP BY object_id, data2; …
Run Code Online (Sandbox Code Playgroud)