Drupal的Views設置中本來就有一個選項是用于去掉重複的,Drupal 6中叫做Distinct:
Defaults: Display only distinct items, without duplicates. Distinct This will make the view display only distinct items. If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work. Note that this can slow queries down, so use it with caution.
而在Drupal 7中這個選項是放在Advanced -> Query settings:Settings,這裡面比Drupal 6還多了一個Pure Distinct子選項:
Distinct This will make the view display only distinct items. If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work. Note that this can slow queries down, so use it with caution. Pure Distinct This will prevent views from adding the base column to the distinct field. If this is not selected and the base column is a primary key, then a non-pure distinct will not function properly because the primary key is always unique.
設置的幫助中可以看到,都說到了“it does not always work”,也就是不能100%保證有效。
我們現在的需求是在一個美國名錄站點中在隻有具體名錄數據(每條包含公司名、所屬city、county、state、sic行業代碼等)的情況下,把county下的city列出來,以及把county下的sic行業代碼列出來,因為views的FILTER CRITERIA是Content: Type (= Business Directory),每個公司的名稱等信息不一樣,雖然可以把Content: County作為CONTEXTUAL FILTERS,在顯示的FIELDS中也隻留下Content: City,但最後顯示出來的列表卻是有很多重複的city(因為一個city有很多名錄頁面),這不符合我們的需求,即使用到Distinct也不行。
這幾天在做Drupal 6到Drupal 7的升級設置,在Drupal 7中設置遇到問題,但在Drupal 6中輸出結果卻是正确的,仔細查看後發現幾年前在設置Drupal 6的時候就想辦法解決了這個問題,現在才回想起來,于是把辦法記錄在博客中:
- 使用Unformatted list格式,在Settings中将Grouping field Nr.1設置為Content: City,這樣列表中就會顯示分組的City鍊接了;
- 再把FIELDS中的Content: City設置為Exclude from display,也就是不顯示每個頁面的City信息了;
- 但如果FIELDS中沒有任何顯示項就會有報錯提示:Display "Page" uses fields but there are none defined for it or all are excluded.,這時候可以添加一個Global: Custom text,裡面什麼内容也沒有就可以了。
這個辦法從顯示上是可以實現需要的效果(例子見:http://ak.mingluji.com/zh-hans/city_of_county/Aleutians%20West ),不過在效率上肯定不高,如果是數據量少的網站沒有大的影響,如果是數據量大的站點就可能對服務器造成比較大的負擔,要根據實際情況來取舍采用。
评论1
usa站點中的city_of_state/修改的方法:
usa站點中的city_of_state/修改的方法: 1)、使用Unformatted list格式,在Settings中将Grouping field Nr.1設置為(term) Taxonomy term: Name 2)、field中的(term) Taxonomy term: Name選擇為Exclude from display 3)、在field中增加Global: Custom text,防止頁面報錯