返回值进行比较

时间:2019-02-07 00:39:31

标签: sql sqlite

该数据库具有模式学生(名称TEXT,math_grade INTEGER,physics_grade INTEGER)。我想选择所有学生的姓名,并选择一个列,如果有另一个学生的数学和物理成绩相同,则该条目为1,否则为0。如何编写查询?

2 个答案:

答案 0 :(得分:1)

嗯。 。 。我在考虑caseexists

select s.*,
       (case when exists (select 1
                          from students s2
                          where s2.math_grade = s.math_grade and
                                s2.physics_grade = s.physics_grade and
                                s2.name <> s.name
                          )
             then 1 else 0
        end) as is_same_flag
from students s;

答案 1 :(得分:0)

自我加入:

select s1.name, case when max(s2.name) is not null then 1 else 0 end
from students s1
left join students s2 on s2.name <> s1.name and s2.math_grade=s1.math_grade and s2.physics_grade=s1.physics_grade
group by s1.name