用量
explore: view_name_1 {
join: view_name_2 {
sql_where: ${view_name_1.id} < 100 ;;
}
}
explore: view_name_1 {
join: view_name_2 {
sql_where: ${view_name_1.id} < 100 ;;
}
}
|
Hierarchy
sql_where |
Default Value
None
Accepts
A SQL WHERE clause
|
sql_where lets you apply a query restriction that users cannot change. The restriction will be inserted into the WHERE clause of the underlying SQL that Looker generates if and only if the join is used in the query. In addition to queries run by human users, the restriction will apply to dashboards, scheduled Looks, and embedded information that relies on that Explore.
The condition can be written in pure SQL, using your database's actual table and column names. It can also use Looker field references like ${view_name.field_name}, which is the preferred method, because Looker can be smarter about automatically including necessary joins. A sql_where condition is not displayed to the user, unless they look at the underlying SQL of any queries that they create.
For example, you can specify that if the join to users is used, that only users younger than 50 should be included:
explore: orders_users_under_50 {
view_name: orders
join: users {
sql_on: ${users.id} = ${orders.user_id} ;;
sql_where: ${users.age} < 50 ;;
type: left_outer
}
}
如果使用者選取 Orders.Count 和 Users.Count,Looker 會從這個 LookML 產生下列 SQL:
SELECT
COUNT(orders.id) AS orders_count,
COUNT(DISTINCT users.id, 1000) AS users_count
FROM thelook2.orders AS orders
LEFT JOIN thelook2.users AS users ON users.id = orders.user_id
WHERE users.age < 50
LIMIT 500
OR 邏輯,則必須加上括號如果您使用 OR 邏輯搭配 sql_where,請務必在 SQL 條件周圍加上半形括號。舉例來說,您不應使用下列指令:
sql_where: region = 'Northeast' OR company = 'Altostrat' ;;
您會寫入下列內容:
sql_where: (region = 'Northeast' OR company = 'Altostrat') ;;
如果忘記在這個範例中加入半形括號,且使用者新增了自己的篩選條件,產生的 WHERE 子句可能就會採用以下形式:
WHERE
user_filter = 'something' AND
region = 'Northeast' OR
company = 'Altostrat'
在這種情況下,使用者套用的篩選器可能無法運作。無論如何,系統都會顯示含有 company = 'Altostrat' 的資料列,因為系統會先評估 AND 條件。如果沒有括號,只有部分 sql_where 條件會與使用者的篩選器合併。如果加入括號,WHERE 子句會改為如下所示:
WHERE
user_filter = 'something' AND
(region = 'Northeast' OR company = 'Altostrat')
現在系統會為每個資料列套用使用者的篩選器。
sql_where依附元件很重要一般來說,無論 LookML 中定義的聯結順序為何,Looker 都會以正確順序實作聯結。但 sql_where 是例外狀況。如果您在 sql_where 陳述式中參照其他聯結的欄位,則參照的聯結必須在 LookML 的 sql_where 陳述式之前定義。
舉例來說,以下 sql_where 陳述式會參照 inventory_items 中已聯結的 inventory_items.id 欄位:
explore: orders {
hidden: yes
join: order_items {
sql_on: ${order_items.order_id} = ${orders.id} ;;
sql_where: ${inventory_items.id} IS NOT NULL ;;
}
join: inventory_items {
sql_on: ${inventory_items.id}=${order_items.inventory_item_id} ;;
}
}
如果您在這個「探索」中執行查詢,Looker 會傳回找不到 inventory_items.id 欄位的錯誤。
不過,您可以重新排序聯結,讓 sql_where 陳述式中參照的聯結在 sql_where 陳述式之前定義,藉此解決這個問題,如下所示:
explore: orders {
hidden: yes
join: inventory_items {
sql_on: ${inventory_items.id}=${order_items.inventory_item_id} ;;
}
join: order_items {
sql_on: ${order_items.order_id} = ${orders.id} ;;
sql_where: ${inventory_items.id} IS NOT NULL ;;
}
}
這樣就能避免發生錯誤,因為 inventory_items 聯結是在 order_items 聯結的 sql_where 陳述式中參照 inventory_items.id 欄位之前定義。
sql_where 查詢限制只有在查詢中使用聯結時,Looker 產生的基礎 SQL 的 WHERE 子句中,才會插入 sql_where 中指定的查詢限制。如要套用 where 子句,即使不會使用聯結也一樣,請改用 sql_always_where。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2026-02-05 (世界標準時間)。