首页 > 解决方案 > 通过连接和多个where子句MYSQL获取记录

问题描述

我在这两个表中有以下记录。

表-A

       Question_No 
           1
           2
           3
           4
           5

表-B

Roll_No  Question  Ans_Option
 1001      1         NULL // NULL means not attempted
 1001      2         D
 1001      3         NULL // NULL means not attempted
 1002      1         C
 1002      2         NULL // NULL means not attempted

当学生跳过问题时,此处“NULL”被显式插入到 Ans_Option 列中。

如何通过mysql查询显示以下结果?

       Questions not attempted by roll no 1001 are : 1, 3, 4, 5 
       Questions not attempted by roll no 1002 are : 2, 3, 4, 5 

我尝试使用以下代码但无法正常工作,如何使用多个 where 子句?

String skipped_Question="NULL";

String selectSQL = "select a.* from Table_A a NATURAL LEFT JOIN Table_B b 
where b.Question IS NULL or b.Ans_Option=? and b.Roll_No=?";// Here I am doing wrong I think

 preparedStatement = conn.prepareStatement(selectSQL);

 preparedStatement.setString(1, skipped_Question);
 preparedStatement.setString(2, Roll_No);

            try{

            rs = preparedStatement.executeQuery();
            while(rs.next())
            {

      Not_Attempted_Questions= rs.getString(1);

         list.add(Not_Attempted_Questions);

            }
            }
            catch(SQLException e){    }
    out.println(list);

非常感谢提前

标签: javamysqlsqljoinwhere-clause

解决方案


推荐阅读