简体   繁体   English

从两个表中选择相同的列值

[英]Selecting same column values from two tables

I am doing one project on struts in which i want to compare two table's row values and want to get matched columns as result set and from result set i want to take out no of matched columns. 我正在struts上做一个项目,在其中我想比较两个表的行值,并希望获得匹配的列作为结果集,而我想从结果集中不取出匹配的列。 I am doing the following coding but i always gets 3 as columnsNumber. 我正在执行以下编码,但我总是将3作为columnNumber。 Please help me to get exact result. 请帮助我获得确切的结果。

public class ResultDAO {


 public int correctAnswer() throws Exception {

     int columnsNumber=0;

     Connection con=null;       
     PreparedStatement pstmt = null;

     ResultSet rs = null;
     String query="SELECT DISTINCT q1,q2,q3 FROM test WHERE (q1,q2,q3)  IN (SELECT q1,q2,q3 FROM result)ORDER BY q1,q2,q3 ASC";
     try {

         con=DatabaseConnection.getConnection();

         pstmt = con.prepareStatement(query);

         rs = pstmt.executeQuery();

         ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();

         columnsNumber = rsmd.getColumnCount();
         System.out.println(columnsNumber);


    } 
     catch (Exception e) {
         System.out.println("exception in DAO");
    }
    return columnsNumber;


 }
}

Table1 : test 表1 :测试

columns: id, q1, q2, q3 values : 1,A,C,C 列:id,q1,q2,q3值:1,A,C,C

Table2 : result 表2 :结果

columns: id, q1, q2, q3 values : 1,A,B,C 列:id,q1,q2,q3值:1,A,B,C

I am using this code but getting value as null 我正在使用此代码,但将值获取为null

public class ResultDAO { 公共类ResultDAO {

 public String correctAnswer() throws Exception {

     String columnsNumber = null;

     Connection con=null;       
     PreparedStatement pstmt = null;

     ResultSet rs = null;
     String query="SELECT test.id,IF (test.q1 = result.q1, test.q1, NULL) as q1,IF (test.q2 = result.q2, test.q2, NULL) as q2,IF (test.q3 = result.q3, test.q3, NULL) as q3,(test.q1 = result.q1) + (test.q2 = result.q2) + (test.q3 = result.q3) as matchedColumns FROM test INNER JOIN result USING (id) ORDER BY q1,q2,q3";
     try {

         con=DatabaseConnection.getConnection();

         pstmt = con.prepareStatement(query);

         rs = pstmt.executeQuery();

         System.out.println(rs.next());
         while(rs.next()){
        columnsNumber=rs.getString("matchedColumns");


         }
         System.out.println(columnsNumber);


    } 
     catch (Exception e) {
         System.out.println("exception in DAO");
    }
    return columnsNumber;


 }

} }

Please try this sqlFiddle 请尝试此sqlFiddle

SELECT test.id,
       IF (test.q1 = result.q1, test.q1, NULL) as q1,
       IF (test.q2 = result.q2, test.q2, NULL) as q2,
       IF (test.q3 = result.q3, test.q3, NULL) as q3,
       (test.q1 = result.q1) + (test.q2 = result.q2) + (test.q3 = result.q3) as matchedColumns
FROM test
INNER JOIN result
USING (id)
ORDER BY q1,q2,q3

edit: to get unmatched columns, just change the = sign to != like this and add another created column named unmatchedColumns sqlFiddle 编辑:要获取不匹配的列,只需将=符号更改为!=然后添加另一个名为unmatchedColumns sqlFiddle的已创建列

SELECT test.id,
       IF (test.q1 = result.q1, test.q1, NULL) as q1,
       IF (test.q2 = result.q2, test.q2, NULL) as q2,
       IF (test.q3 = result.q3, test.q3, NULL) as q3,
       (test.q1 = result.q1) + (test.q2 = result.q2) + (test.q3 = result.q3) as matchedColumns,
       (test.q1 != result.q1) + (test.q2 != result.q2) + (test.q3 != result.q3) as unmatchedColumns
FROM test
INNER JOIN result
USING (id)
ORDER BY q1,q2,q3

I am assuming id is foreign key in result table 我假设id是结果表中的外键

 select tst.id,tst.q1,tst.q2,tst.q3,
    r.id,r.q1,r.q2,r.q3
     from test as tst
    LEFT JOIN result AS r
    ON tst.id = r.id

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

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