I have a task to create a view that will add a column which checks if data in two rows of one table matches the same two rows of another table.
I have these tables:
Table 1
Name ID Team
John 35 1
Josh 89 1
Josh 89 2
Josh 89 3
Jim 25 2
Table2
Name ID Team
Josh 89 1
John 35 1
I need to create a view adds a column which places a number 1 whenever table1.id and table1.team match table2.id and table2.team. Like this:
NEW view
NAME ID TEAM CHECK
John 35 1 1
Josh 89 1 1
Josh 89 2 0
Josh 89 3 0
Jim 25 2 0
However, the closest I can get is this code:
Select distinct
table1.name, table1.id, table1.team,
CASE
WHEN
(table1.id = table2.id)
AND
(table1.team = table2.team)
THEN 1
ELSE 0
END AS check
FROM
table1, table2 order by check desc;
Which returns:
NEW view
NAME ID TEAM CHECK
John 35 1 1
Josh 89 1 1
John ---- 35 ----- 1 ----- 0
Josh ---- 89 ----- 1 ----- 0
Josh 89 2 0
Josh 89 3 0
Jim 25 2 0
without the distinct I get duplicates of every row and with distinct I get one row labled 0 for every row that's labled 1
I tried:
Select a.id, a.team,
CASE
WHEN
( select a.id from v_admins a
Where
a.id = (select o.id from oncallorder o)
And
a.team = ( select o.team from oncallorder o)
)
THEN 1
ELSE 0
END AS adminisoncall
FROM v_admins a, oncallorder o
order by adminisoncall desc;
but it just errors out with: Error Code: 1242. Subquery returns more than 1 row
any thoughts?