简体   繁体   中英

select rows whose value matches with either of the two columns in the second table:

I have a table that looks like this.

id        name
1         firstName
2         secondName
3         thirdName
4         fourthName

I want to keep all rows where the name is present in either the "testName" or "prodName" columns from the second table that looks like this:

testName        prodName            coordinates
firstName       EN                  124
random          secondName          1244
thirdName       DE                  689
FifthName       DE                  457

I want a resulting table that looks like this:

id        name                coordinates
1         firstName           124
2         secondName          1244
3         thirdName           689

Something like this worked if i was comparing with only one other column from table2:

(select * 
  from `table1`
  where exists 
    (select 1
      from `table2` where testName = `table1`.name ));

but this would throw an error:

(select * 
  from `table1`
  where exists 
    (select 1
      from `table2` where testName = `table1`.name OR prodName = `table1`.name ));
LEFT SEMI JOIN cannot be used without a condition that is an equality of fields from both sides of the join.

Please try this:

SELECT 
A.id
,A.name
,B.coordinates

FROM table1 A
INNER JOIN table2 B
ON A.name=B.testName OR A.name=B.prodName

You told that you need the field coordinate as well and that is not unique in table2. Here an example for obtaining the maximum of coordinate . There is no such thing as first entry in a database, only the value with the first date in another column etc. You did not specify that!

With table1 as 
(Select row_number() over() as id, * from unnest(["firstName","secondName","thirdName","fourthName"]) name
),table2 as 
(select * from
(select "firstName" as testname, "NB" as prodName
union all select "BG","secondName") ,
unnest(generate_array(1,1)) as coordinates
)
SELECT 
A.id
,A.name
,max(B.coordinates),
max(C.coordinates) as C_coordinates,
avg(ifnull(B.coordinates,C.coordinates)) as coordinate_new,

FROM table1 A
left JOIN table2 B
ON A.name=B.testName 
left JOIN table2 C
ON A.name=C.prodName
group by 1,2
having coordinate_new is not null

Here is the example with the Where filter:

With table1 as 
(Select row_number() over() as id, * from unnest(["firstName","secondName","thirdName","fourthName"]) name
),table2 as 
(select "firstName" as testname, "NB" as prodName
union all select "BG","secondName") 

select * 
  from `table1`
  where exists 
    (select 1 from `table2` where testName = `table1`.name
    union all Select 1 from table2 where prodName = `table1`.name )
create temp table t1 as(
 select "firstName" as  testName ,"EN" as prodName , 124 as coordinates union all
 select "random","secondName",1244 union all
 select "thirdName","DE",689 union all
 select "FifthName","DE",457
);
 
create temp table t2 as(
select 1 as id,"firstName" as name,124 as coordinates union all
select 2,"secondName",1244 union all
select 3,"thirdName",689
);
 
 
create temp table t3 as(
select a.id as id,a.name as name from  t2 as a where a.id<=0 or exists (select coordinates from t1 as b1 where a.name = b1.testName or a.name=b1.prodName)
);
 
select t3.id,t3.name,t2.coordinates from t3 inner join t2 on t3.name=t2.name;

The query is based on whatever data you have provided in the question. If this does not satisfy your requirement then please edit the question and add some more details with sample input, output data of your requirement.

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