假設前綴(當然,後綴)總是三個字符,您可以執行與SUBSTR
結果的有效半連接:
#standardSQL
WITH Girls AS (
SELECT name
FROM UNNEST(['Le-na', 'Lo-ve', 'Li-na', 'Lu-na', 'Len-ka']) AS name
),
Suffixes AS (
SELECT suffix
FROM UNNEST(['-na', '-ve', '-la', '-ta', '-ya']) AS suffix
)
SELECT
name
FROM Girls
WHERE EXISTS (
SELECT 1 FROM Suffixes WHERE suffix = SUBSTR(name, LENGTH(name) - 2)
);
或者你可以使用LIKE
,但它相當於執行交叉帶過濾器聯接,所以它可能不會那麼快:
#standardSQL
WITH Girls AS (
SELECT name
FROM UNNEST(['Le-na', 'Lo-ve', 'Li-na', 'Lu-na', 'Len-ka']) AS name
),
Suffixes AS (
SELECT suffix
FROM UNNEST(['-na', '-ve', '-la', '-ta', '-ya']) AS suffix
)
SELECT
name
FROM Girls
WHERE EXISTS (
SELECT 1 FROM Suffixes WHERE name LIKE CONCAT('%', suffix)
);
編輯:
0:枚舉所有名稱後綴使用的半連接的另一種選擇
#standardSQL
WITH Girls AS (
SELECT name
FROM UNNEST(['Le-na', 'Lo-ve-lala', 'Li-na', 'Lu-eya', 'Len-ka']) AS name
),
Suffixes AS (
SELECT suffix
FROM UNNEST(['-na', '-ve', '-lala', '-ta', '-eya']) AS suffix
),
GirlNamePermutations AS (
SELECT name, SUBSTR(name, LENGTH(name) + 1 - len) AS name_suffix
FROM Girls
CROSS JOIN UNNEST(GENERATE_ARRAY(1, (SELECT MAX(LENGTH(suffix)) FROM Suffixes))) AS len
)
SELECT
name
FROM GirlNamePermutations
WHERE EXISTS (
SELECT 1
FROM Suffixes
WHERE suffix = name_suffix
);
如果知道後綴長度的範圍,則可以改爲對其進行硬編碼,例如,更換:
CROSS JOIN UNNEST(GENERATE_ARRAY(1, (SELECT MAX(LENGTH(suffix)) FROM Suffixes))) AS len
有:
CROSS JOIN UNNEST(GENERATE_ARRAY(1, 5)) AS len
非常感謝您的建議。以下是google-bigquery響應: ON子句必須是AND表達式=每個表的一個字段名稱的比較,所有字段名稱都以表名爲前綴。' –
確保啓用[標準SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/),例如通過在查詢之前放置'#standardSQL'。 –
非常感謝,先生! Elliott,祝你有10/10天和一週! –