简体   繁体   中英

Using LIKE between 2 columns in Big Query

I am having 2 tables ie tableA and tableB . I am fetching colA from tableA and colB from tableB . Now I want to join tableA and tableB on colA where colA is LIKE colB.

eg if colB is having a value " abcxyzijk " and colA is having a value " xyz " then also it should join. The join should not be limited to colA = colB .

I tried using the below code but it's giving only the records for which colA matches exactly with colB.

SELECT tableA.colA,  tableB.colB
FROM TableB tableB
INNER JOIN TableA tableA
ON tableB.colB LIKE tableA.colA

Is there any way of using LIKE between two columns in Big Query?

You can instead use CROSS JOIN then filter the data using HAVING and REGEXP_CONTAINS .

WITH Roster AS
 (SELECT 'Adams' as LastName, 'asdf' as SchoolID UNION ALL
  SELECT 'Buchanan', 'qwe' UNION ALL
  SELECT 'Coolidge', 'zxc' UNION ALL
  SELECT 'Davis', 'eryvbnqc' UNION ALL
  SELECT 'Eisenhower', 'pyimkmhjrty')

, TeamMascot AS
 (SELECT 'asdf' as SchoolID, 'Jaguars' as Mascot UNION ALL
  SELECT 'qwe', 'Knights' UNION ALL
  SELECT 'zxc', 'Lakers' UNION ALL
  SELECT 'vbn', 'Mustangs' UNION ALL
  SELECT 'rty', 'Cavs')

SELECT ros.LastName,ros.SchoolID, mas.SchoolID,mas.Mascot FROM Roster as ros
CROSS JOIN TeamMascot as mas
GROUP BY 1,2,3,4
HAVING REGEXP_CONTAINS(ros.SchoolID, mas.SchoolID)

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM