sql x= 时的情况?那么y=?结尾

sql case when x=? then y=? end

我有一个 sql 案例的问题,我想检查一个列值,如果 returns 为真,那么我想更改另一个列值,但是在 'case' 您只能检查一列并仅更改该列的值,我该如何让它工作?

select f.[film name],f.city,p.[participant name],
case rt.[type name]
when 'director' then 'director' else null
end 'role type'
from Films f 
join FilmParticipants fp on fp.filmID=f.filmID 
join Participants p on p.participantID=fp.participantID 
join RoleType rt on rt.roleID=fp.roleID
where f.city in(
select f.city
from Films f
group by f.city
having COUNT(*)>1)
order by f.city

它给出了这个 table:

film name   | city       | participate name | role type
Shrek       | London     | John             | null
Shrek       | London     | John             | null
Dragon      | London     | Rick             | null
Drago       | London     | Morty            | Director

现在我希望只要 'role type column' 中有 null,'participate name' 列也将为 null。

您可以使用 case when 以相同的方式进行角色类型和显示 partecipant.name

      select f.[film name],f.city,
       case rt.rt.[type name]
           when 'director' then p.[participant name] else null end 'partecipant',
      case rt.[type name]
           when 'director' then 'director' else null
      end 'role type'
      from Films f 
      join FilmParticipants fp on fp.filmID=f.filmID 
      join Participants p on p.participantID=fp.participantID 
      join RoleType rt on rt.roleID=fp.roleID
      where f.city in(
        select f.city
        from Films f
        group by f.city
        having COUNT(*)>1)

      order by f.city

删除 null

      select [film name], city,  max(partecipant), max([role type])
      from (
      select f.[film name],f.city,
       case rt.rt.[type name]
           when 'director' then p.[participant name] else null end 'partecipant',
      case rt.[type name]
           when 'director' then 'director' else null
      end 'role type'
      from Films f 
      join FilmParticipants fp on fp.filmID=f.filmID 
      join Participants p on p.participantID=fp.participantID 
      join RoleType rt on rt.roleID=fp.roleID
      where f.city in(
        select f.city
        from Films f
        group by f.city
        having COUNT(*)>1)
      order by f.city ) t 
      group by [film name], city