[Solved] Cause: java.sql.SQLException: The used SELECT statements have a different number of columns

1. Problems: 

When multiple select queries are used for union, the SQL error message is as follows

### Cause: java.sql.SQLException: The used SELECT statements have a different number of columns
; bad SQL grammar []; nested exception is java.sql.SQLException: The used SELECT statements have a different number of columns
	at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101) ~[spring-jdbc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.8.RELEASE.jar:5.1.8.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.1.8.RELEASE.jar:5.1.8.RELEASE]

2. Solutions

This is because the table structure of the records generated by the two SQL statements using union is inconsistent. Only the records set with completely consistent structure can use union. The above is caused by the different fields of the two tables, so you can check and solve it

Similar Posts: