在列中不相等时使用“IN”

时间:2013-12-10 06:17:14

标签: sql postgresql

如果栏中不相等,我怎么能使用“IN”。

LEFT JOIN (
     SELECT student_id 
     FROM students 
     WHERE student_id  IN (
             SELECT student_id,
                    CASE 
                       WHEN (
                          //condition           
                       )
                       THEN 'Yes'
                       ELSE 'No'
                    END AS refusal
             FROM refusal_field
 )
) AS a USING(student_id)

它说:子查询有太多列

我真的需要“拒绝”专栏给我LEFT JOIN。怎么做?

2 个答案:

答案 0 :(得分:1)

这样的事情怎么样?这应该有你想要的结果:

LEFT JOIN (
SELECT student_id,
CASE 
WHEN (
//condition         
)
THEN 'Yes'
ELSE 'No' 
END AS refused
FROM students 
INNER JOIN refusal_field ON refusal_field.student_id = students.student_id) 

答案 1 :(得分:1)

实际上,子查询有两列:student_id和拒绝,它应该只有student_id, 我相信你想要的是:

LEFT JOIN (
   SELECT 
      student_id,
      CASE 
         WHEN (
          //condition         
         ) THEN 'Yes'  ELSE 'No' END AS refusal
   FROM students WHERE student_id  IN (
      SELECT student_id
      FROM refusal_field
   )
) AS a USING(student_id)