我有两个表 api_user 和 api_user,其中 api_user 具有用户表的外键。这两个表的架构如下所列。
Table "public.api_user"
Column | Type | Modifiers
--------------+--------------------------+-------------------------------------------------------
id | integer | not null default nextval('api_user_id_seq'::regclass)
is_admin | boolean | not null
is_agent | boolean | not null
is_guide | boolean | not null
Indexes:
"api_user_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "api_userprofile" CONSTRAINT "api_userprofile_user_id_5a1c1c92_fk_api_user_id" FOREIGN KEY (user_id) REFERENCES api_user(id) DEFERRABLE INITIALLY DEFERRED
Table "public.api_userprofile"
Column | Type | Modifiers
------------------------+-------------------------+--------------------------------------------------------------
id | integer | not null default nextval('api_userprofile_id_seq'::regclass)
percent_complete | numeric(3,0) | not null
display_name | character varying(128) | not null
city | character varying(64) | not null
user_id | integer | not null
Indexes:
"api_userprofile_pkey" PRIMARY KEY, btree (id)
"api_userprofile_user_id_key" UNIQUE CONSTRAINT, btree (user_id)
Foreign-key constraints:
"api_userprofile_user_id_5a1c1c92_fk_api_user_id" FOREIGN KEY (user_id) REFERENCES api_user(id) DEFERRABLE INITIALLY DEFERRED
当我运行以下查询时:
select
api_user.id,
api_userprofile.display_name,
api_userprofile.city
FROM "api_user"
LEFT OUTER JOIN "api_userprofile" ON ("api_user"."id" = "api_userprofile"."user_id")
WHERE ((("api_user"."is_admin" = false
AND "api_userprofile"."percent_complete" >= 60.0
AND "api_userprofile"."id" IS NOT NULL))
AND "api_user"."is_guide" = true)
ORDER BY "api_userprofile"."city" ASC LIMIT 20;
它返回:
id | display_name | city
-----+---------------------------+--------------------------
299 | Mohsin Khan | Agra
93 | Rizwan Mohd | Agra
126 | Abdhesh Sharma | Agra
39 | Rashid Ahmed | Agra
244 | Nishkam Sharma | Ajmer
42 | Parminder Mahla | Amritsar
131 | Prashant Hullatti | Ballry
241 | Pankaj Anand | Bangalore
89 | Niraj K. Singh | Bodhgaya, Nalanda, Patna
204 | Ravi Rocks | Bokaro
19 | Ian Lotriet | Cape Town
15 | Ivy Almacin | Cape Town
38 | Dr Brahm Prakaah Tripathi | Delhi
130 | Virendra Singh | Delhi
271 | Satish Jain | Delhi
110 | Vikas Agarwal | Delhi
114 | Devi Singh Rathore | Delhi
58 | Dilip Singh Chanpawat | Delhi
95 | Anam Kumar Dhasmana | Delhi
51 | Gopal Sharma | Delhi
使用偏移量 20 返回再次运行查询:
id | display_name | city
-----+---------------------------+------------
95 | Anam Kumar Dhasmana | Delhi
114 | Devi Singh Rathore | Delhi
252 | Tarun Pratap Singh | Delhi
258 | Rajesh Kumar Pal | Delhi
255 | Chandan Singh Shekhawat | Delhi
268 | Amit Kumar | Delhi
100 | Ketan Mehra | Delhi
286 | Vikash Poonia | Delhi
61 | Belinda Schempers | Durban
67 | Pieter Janse Van Rensburg | Hoedspruit
140 | Dr Hari Krishna Somanchi | Hyderabad
197 | Sstya Prabha | Hyderabad
118 | Dalpat Jodha | Jaipur
253 | Yash Shekhawat | Jaipur
120 | Govind Sharma | Jaipur
257 | Abhimanyu Singh | Jaipur
99 | Ghanshyam Singh | Jaisalmer
308 | Nitin Lobo | Jodhpur
124 | Rajendra Singh | Jodhpur
55 | Umed Gehlot | Jodhpur
从输出中可以看出,在第一个查询和下一个偏移量为 20 的查询中都返回了一些重复项(参见 ID 为 114 的用户)。
使用 distinct 似乎工作正常,但为什么它在按相关表(用户配置文件)上的字段排序时返回重复项?
显然,如果我按 user.id 订购,那么它似乎也能正常工作并且不会返回重复项。
这里 user 和 userprofile 之间的关系是一对一的,没有 user.id 在 userprofile.user_id 中被多次引用(由框架(django)强制执行)。
这里没有“重复”,你运行了两次查询。如果您仅按城市排序,则不会定义同一城市(例如德里)内 ID 和名称的顺序。因此,城市内的结果每次都可能以不同的顺序返回。