简体   繁体   English

如何在 sql 中实现这一点?

[英]how to achieve this in sql?

I have a two table table我有一张两张桌子

Table1:表格1:

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:表 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我想在表 1 中插入名为“is_captain”和“is_vicecaptain”的列,并在表 2 的帮助下,如果是,则为 1,如果是,则为 0 用于参加运动=足球的人

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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