I have a two table table
Table1:
id | name | score | sports
hh-a | ben | 67 | football
gy-a | jen | 98 | criket
df-r | jay | 56 | football
jf-a | mic | 87 | cricket
yu-r | bal | 98 | football
gt-a | gal | 96 | football
dr-a | pal | 46 | football
Table 2:
captain_name |vicecaptain | sports
df-r | gt-a | football
I want to insert columns in table 1 named "is_captain" and "is_vicecaptain" and with the help of table 2 put 1 if yes else 0 for whoever plays sports= football
SELECT table1.*,
COALESCE(table1.id = table2.captain_name, 0) is_captain,
COALESCE(table1.id = table2.vicecaptain, 0) is_vicecaptain
FROM table1
LEFT JOIN table2 ON table1.id IN (table2.captain_name, table2.vicecaptain)
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.