所以我的语法在所有三种情况下显然是正确的(PostgreSQL并没有对任何东西感兴趣),但结果以所有这三种查询的相同顺序返回。甚至更奇怪的是,当我从下面的任何一个添加/删除DESC时,它也没有影响。是否可以根据子查询的元素对结果进行排序?子查询和排序? (ORDER BY)
Sort by affiliation
SELECT * FROM articles_view WHERE (1=1)
AND spubid IN
(SELECT people.spubid FROM people WHERE (people.slast ilike 'doe')
GROUP BY people.spubid, people.slast, people.saffil)
AND spubid IN
(SELECT status.spubid FROM status WHERE ((status.imonth >= 01 OR status.imonth IS NULL) AND status.iyear >= 2000) AND ((status.imonth <= 01 OR status.imonth IS NULL) AND status.iyear <= 2008) ORDER BY status.iyear, status.imonth)
Sort by last name, descending order
SELECT * FROM articles_view WHERE (1=1)
AND spubid IN
(SELECT people.spubid FROM people WHERE (people.slast ilike 'doe')
GROUP BY people.spubid, people.slast, people.saffil ORDER BY people.slast DESC)
AND spubid IN
(SELECT status.spubid FROM status WHERE ((status.imonth >= 01 OR status.imonth IS NULL) AND status.iyear >= 2000) AND ((status.imonth <= 01 OR status.imonth IS NULL) AND status.iyear <= 2008))
Sort by year/month descending order
SELECT * FROM articles_view WHERE (1=1)
AND spubid IN
(SELECT people.spubid FROM people WHERE (people.slast ilike 'doe')
GROUP BY people.spubid, people.slast, people.saffil)
AND spubid IN
(SELECT status.spubid FROM status WHERE ((status.imonth >= 01 OR status.imonth IS NULL) AND status.iyear >= 2000) AND ((status.imonth <= 01 OR status.imonth IS NULL) AND status.iyear <= 2008) ORDER BY status.iyear, status.imonth DESC)
我只是不确定为什么ORDER BY条件对结果顺序没有影响。
********* UPDATE:
我最终什么事是使用阵列列在我看来,(在这种情况下articles_view)做我的所有排序做。这样我就可以在主要查询中的“列”上完成所有工作,完全避免使用JOINS。定义视图的方式是,所有与人员/状态表中的给定pubid(主键)匹配的列(都具有1->多个)存储在视图中的数组列中。我与排序查询看起来是这样的:
SELECT * FROM articles_view WHERE
((articles_view.skeywords_auto ilike '%ice%') OR (articles_view.skeywords_manual ilike '%ice%'))
ORDER BY (articles_view.authors[1]).slast
这部作品的原因是因为我总是知道该数组的第一个成员(从Postgres第一指数为1,而不是通常0),是主要的作者(或主要地位),这是我需要排序。
呃这就是我害怕的,我真的想避免使用连接(产品会变得太大,因此使用子查询来代替)。 – 2009-08-19 23:23:08
使用内部联接您只会得到合适的结果,并且查询优化器应该确定一个计划,在应用联接之前删除不满足where子句的任何内容。 – 2009-08-19 23:30:25