Google BigQuery - 将列中的特定单词与不同表中另一列中的文本进行匹配

时间:2018-02-15 18:20:27

标签: sql google-bigquery

我正在尝试匹配SQL中不同表格中的不同信息。

我有2张桌子。 表1包含1列words,其中包含爱,激情,工作等字样,以及包含1列text的表2,其中包含新闻片段。

我在Google Cloud Platform中使用Big Query,我需要一个查询来匹配words

中的text个{}

我已尝试过此查询,但无效

SELECT b.word
    ,a.text
FROM Table1 b
INNER JOIN Table2 a ON ' '+ b.word + ' ' LIKE '% ' + a.text + ' %';

我有这个信息 [表格中的信息]

我想得到这个 [结果预期]

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

下面的示例适用于BigQuery Standard SQL

因为你错过了提供可读的输入/输出 - 我只是使用帖子的文字来构成虚拟数据

希望下面的例子显示你正确的方式

#standardSQL
WITH table1 AS (
  SELECT 'love' AS word UNION ALL
  SELECT 'passion' UNION ALL
  SELECT 'job' UNION ALL
  SELECT 'cloud'
), table2 AS (
  SELECT '''I’m trying to match different information from different tables in SQL.''' text UNION ALL
  SELECT '''I have 2 tables. Table1 with 1 column “words” that iclude words like love, passion, job... And Table2 with 1 column “text” that include a fragment of a News.''' UNION ALL
  SELECT '''I am using Big Query in Google Cloud Platform and I need a Query to match which “words” are in the “text”''' UNION ALL
  SELECT '''I have tried this query, but it doesn't work''' UNION ALL
  SELECT '''SELECT b.word ,a.text FROM Table1 b INNER JOIN Table2 a ON ' '+ b.word + ' ' LIKE '% ' + a.text + ' %';''' UNION ALL
  SELECT '''I have this Information [Information in the tables]''' UNION ALL
  SELECT '''And I want to get this [Result expected]''' UNION ALL
  SELECT '''Thanks for your help!''' 
)
SELECT word, text
FROM table1 b
JOIN table2 a ON REGEXP_CONTAINS(text, CONCAT(r'(?i)\b', word, r'\b'))
-- ORDER BY text, word

详细了解regular expression syntax

答案 1 :(得分:0)

#standardSQL
WITH
  Table1 AS (
  SELECT
    "a" AS word
  UNION ALL (
    SELECT
      "b" AS word)),
  Table2 AS (
  SELECT
    "abc" AS text
  UNION ALL (
    SELECT
      "acn" AS text))
SELECT
  a.word,
  b.text
FROM
  Table1 a
JOIN
  Table2 b
ON
  b.text LIKE CONCAT('%',a.word,'%')