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. I am doing the following coding but i always gets 3 as columnsNumber. 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
columns: id, q1, q2, q3 values : 1,A,C,C
Table2 : result
columns: id, q1, q2, q3 values : 1,A,B,C
I am using this code but getting value as null
public class 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
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
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.