I don`t get it. I created a query where made some joins of two tables in SQLite. In my SQLite Browser there is all ok and the columns are displayed correctly. But when I call executeQuery, I always get the error:
[SQLITE_ERROR] SQL error or missing database (no such column: td.value)
Here is my statement:
private static final String QUERY_ENDUSES = "SELECT td.Value Value, reportn.Value ReportName, fs.Value ReportForString, tn.Value TableName, rn.Value RowName, cn.Value ColumnName, u.Value Units, RowId "
+ "FROM TabularData td"
+ "INNER JOIN Strings reportn ON reportn.StringIndex=td.ReportNameIndex "
+ "INNER JOIN Strings fs ON fs.StringIndex=td.ReportForStringIndex "
+ "INNER JOIN Strings tn ON tn.StringIndex=td.TableNameIndex "
+ "INNER JOIN Strings rn ON rn.StringIndex=td.RowNameIndex "
+ "INNER JOIN Strings cn ON cn.StringIndex=td.ColumnNameIndex "
+ "INNER JOIN Strings u ON u.StringIndex=td.UnitsIndex WHERE report n.StringTypeIndex=1 AND fs.StringTypeIndex=2 AND tn.StringTypeIndex=3 AND rn.StringTypeIndex=4 AND cn.StringTypeIndex=5 AND u.StringTypeIndex=6 "
+ "AND td.ReportNameIndex = 1 AND tn.StringIndex = 59;";
If I substitute the statement through Select td.value from TabularData td , then all is OK!
Can anybody help me?
You're missing a space between td in the FROM clause, and the first INNER.
Related
I have to return two app name source and target so i am joining the application table to get the the name.. Below is the source code. When I run it I get the following query
select distinct app_source,target,zao.app_name,dependency,zat.app_name from zapp_dependency zd join zapplication zao on zao.csi_id=zd.app_source
join zapplication zat on zat.csi_id=zd.target
which when i run in oracle sql developer i get the desired result i.e. the source and target app name as different but in hibernate i get them as same.. Can someone please help?
try
{
Session session=HibernateUtil.currentSession();
String sql="select distinct app_source,target,zao.app_name,dependency,zat.app_name from zapp_dependency zd join zapplication zao "
+ "on zao.csi_id=zd.app_source"
+ " join zapplication zat on zat.csi_id=zd.target ";
ob=session.createSQLQuery(sql).list();
for(Object[] ab:ob)
{
ZAppDependencyDTO elem=new ZAppDependencyDTO();
elem.setSource(ab[0]==null?"":ab[0].toString());
elem.setTarget(ab[1]==null?"":ab[1].toString());
elem.setDependency(ab[3]==null?' ':ab[3].toString().trim().charAt(0));
elem.setId(elem.getSource()+"###"+elem.getTarget()+"###"+elem.getDependency());
elem.setSourcename(ab[2]==null?"":ab[2].toString());
System.out.println(ab[2].toString());
elem.setTargetname(ab[4]==null?"":ab[4].toString());
System.out.println(ab[4].toString());
dto.add(elem);
}
}
catch(Exception ex)
{
count=0;
ex.printStackTrace();
tx.rollback();
}
Seems to be problem with same column name in in two tables
Also mentioned here
Could you try with below, giving specific alias name using {}
String sql="select distinct app_source,target,source_name as {zao.app_name},dependency,target_name as {zat.app_name} from zapp_dependency zd join zapplication zao "
+ "on zao.csi_id=zd.app_source"
+ " join zapplication zat on zat.csi_id=zd.target ";
You should use alias for the same column name of two table.
select distinct app_source,target,zao.app_name as zao_app_name ,zat.app_name as zat_app_name ...
Changed my query to
String sql="select distinct app_source,target,zao.app_name as source_name,dependency,zat.app_name as target_name from zapp_dependency zd join zapplication zao "
+ "on zao.csi_id=zd.app_source"
+ " join zapplication zat on zat.csi_id=zd.target "
and it worked basically aliasing
Hello I am using a JDBC query and I am having this error but I don't understand why, or what exactly is the meaning of this error and what is wrong with the query.
The query is as below
private List<StatisticsLog> runStatistics(Integer partnerId, LocalDate ldStart, LocalDateTime ldtEnd, String statType)
{
return jdbcTemplate.query("SELECT S.APP_ID, S.LOG_TYPE, ACC.CONTACT_PERSON_FIRST_NAME, ACC.CONTACT_PERSON_LAST_NAME, ACV.VERSION_APP_NAME, "
+ "'" + statType.trim() + "' STATTYPE, "
+ "ACV.APP_CATALOG_ID, ACV.APP_CATALOG_VERSION_ID, ACV.VERSION, ACV.UPDATED_AT, ACV.VERSION_LOGO_EXT, ACV.HAS_LOGO, "
+ "LANG.LANGUAGE_NAME_SHORT, LANG.LANGUAGE_ID , S.count, S.PARTNER_ID, APP.CREATED_AT "
+ "FROM (SELECT partner_id, log_type, app_id, language_id, count(*) as count FROM public.statistics_log "
+ " WHERE partner_id = ? "
+ " and logged_at between ? and ? "
+ "group by 1, log_type, app_id, language_id) as S "
+ "INNER JOIN APP_CATALOG_ACCOUNT ACP ON ACP.APP_CATALOG_SERIAL_ID = S.APP_ID "
+ "INNER JOIN APP_CATALOG APP ON APP.APP_CATALOG_SERIAL_ID = S.APP_ID "
+ "INNER JOIN ACCOUNT ACC ON ACC.ACCOUNT_ID = S.PARTNER_ID "
+ "INNER JOIN APP_CATALOG_VERSION ACV on ACV.APP_CATALOG_SERIAL_ID = APP.APP_CATALOG_SERIAL_ID AND ACV.STATUS = 3 "
+ "INNER JOIN LANGUAGE LANG ON LANG.LANGUAGE_ID = ACV.LANGUAGE_ID "
+ "ORDER BY S.count desc ",
new Object[] { partnerId, ldStart, ldtEnd }, new StatisticsLogRowMapper());
}
and I am getting the error from this column CREATED_AT that is part of the app catalog, and I think it should be there, also for more detailed also I will attach StatisticsLogRowMapper
public class StatisticsLogRowMapper implements RowMapper<StatisticsLog>
{
#Override
public StatisticsLog mapRow(ResultSet rs, int rowNum) throws SQLException
{
StatisticsLog stat = new StatisticsLog();
stat.setAppId(rs.getInt("APP_ID"));
stat.setPartnerId(rs.getInt("PARTNER_ID"));
stat.setCount(rs.getLong("COUNT"));
stat.setCreatedAt(rs.getTimestamp("CREATED_AT").toLocalDateTime());
stat.setPartnerFirstName(rs.getString("CONTACT_PERSON_FIRST_NAME"));
stat.setPartnerLastName(rs.getString("CONTACT_PERSON_LAST_NAME"));
stat.setAppNameDefault(rs.getString("VERSION_APP_NAME"));
stat.setAppCatalogId(rs.getInt("APP_CATALOG_ID"));
stat.setStatType(rs.getString("STATTYPE"));
stat.setLogType(LogTypeEnum.valueOf(rs.getString("LOG_TYPE").trim()));
stat.setAppCatalogVersionId(rs.getInt("APP_CATALOG_VERSION_ID"));
stat.setVersion(rs.getInt("VERSION"));
stat.setUpdatedAt(rs.getDate("UPDATED_AT"));
stat.setVersionLogoExt(rs.getString("VERSION_LOGO_EXT"));
stat.setHasLogo(rs.getBoolean("HAS_LOGO"));
stat.setLanguageNameShort(rs.getString("LANGUAGE_NAME_SHORT"));
stat.setLanguageId(rs.getInt("LANGUAGE_ID"));
return stat;
}
}
THE ERROR IS: "PreparedStatementCallback; bad SQL grammar [SELECT S.APP_ID, S.LOG_TYPE, ACC.CONTACT_PERSON_FIRST_NAME, ACC.CONTACT_PERSON_LAST_NAME, ACV.VERSION_APP_NAME, 'days30' STATTYPE, ACV.APP_CATALOG_ID, ACV.APP_CATALOG_VERSION_ID, ACV.VERSION, ACV.UPDATED_AT, ACV.VERSION_LOGO_EXT, ACV.HAS_LOGO, LANG.LANGUAGE_NAME_SHORT, S.count, S.PARTNER_ID FROM (SELECT partner_id, log_type, app_id, language_id, count(*) as count FROM public.statistics_log WHERE logged_at between ? and ? group by 1, log_type, app_id, language_id, partner_id) as S INNER JOIN APP_CATALOG_ACCOUNT ACP ON ACP.APP_CATALOG_SERIAL_ID = S.APP_ID INNER JOIN APP_CATALOG APP ON APP.APP_CATALOG_SERIAL_ID = S.APP_ID INNER JOIN ACCOUNT ACC ON ACC.ACCOUNT_ID = S.PARTNER_ID INNER JOIN APP_CATALOG_VERSION ACV on ACV.APP_CATALOG_SERIAL_ID = APP.APP_CATALOG_SERIAL_ID AND ACV.STATUS = 3 INNER JOIN LANGUAGE LANG ON LANG.LANGUAGE_ID = ACV.LANGUAGE_ID ORDER BY S.count desc ]; nested exception is org.postgresql.util.PSQLException: The column name CREATED_AT was not found in this ResultSet."
IN THE ERROR SEEMS THIS FIELD ISN'T THERE SOMEHOW
If you have any idea what may be wrong or thing I can try please don't hesitate to comment, I would appreciate d even the smallest help thank you.
I can see there is an error in line 2 of the query - + "'" + statType.trim() + "' STATTYPE, ", right after the quote. STATTYPE does not have a table reference and is appended with a string. That could be another issue. I cannot comment on the question as I dont have enough reputation.
I can't select inner join data from MYSQL in Java.
I'm using this code in Java:
Class.forName("com.mysql.jdbc.Driver");
Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost:3306/despesas?useSSL=true", "root", "local");
Statement comando = conexao.createStatement();
String sqlInsert;
ResultSet rsTDP;
sqlInsert = "select id_historico, tipos_de_despesas.descricao, dt_desp, valor_despesa from tipos_de_despesas INNER JOIN historico_despesas ON tipos_de_despesas.id_despesa=historico_despesas.id_despesa ORDER BY dt_desp desc";
rsTDP = comando.executeQuery(sqlInsert);
while(rsTDP.next()){
System.out.println("ID da Despesa: " + rsTDP.getString("id_despesa") + " | Descrição: " + rsTDP.getString("descricao"));
}
This sintax works in MYSQL Workbench but in Java does not.
SELECT id_historico, tipos_de_despesas.descricao, dt_desp, valor_despesa from tipos_de_despesas INNER JOIN historico_despesas ON tipos_de_despesas.id_despesa=historico_despesas.id_despesa ORDER BY dt_desp desc
This is result in Workbench
Can anyone help me?
I'll bet the exception is actually coming from this line:
System.out.println("ID da Despesa: " + rsTDP.getString("id_despesa") + " | Descrição: " + rsTDP.getString("descricao"));
I don't see the id_despesa column in the select statement and the ResultSet can only read columns from the select statement. So the rsTDP.getString("id_despesa") will throw a SQLException.
If you don't need the id_despesa column, then you can just leave it out of the println. Otherwise, you can change your SQL to this:
sqlInsert = "select tipos_de_despesas.id_despesa, id_historico, tipos_de_despesas.descricao, dt_desp, valor_despesa from tipos_de_despesas INNER JOIN historico_despesas ON tipos_de_despesas.id_despesa=historico_despesas.id_despesa ORDER BY dt_desp desc";
With the column added to the query, then you will be able to get id_despesa out of the results.
I'm trying to get result from 3 inner join in java code with sql.
I tried this
BeanHandler rsh=new BeanHandler(evaluation.class);
evaluation eval=(evaluation) runner.query(query,new Object[]{userid}, rsh);
This code return the result from query, but with "join" statement it's not working, I want to get result from 3 tables.
any ideas?
Edit: (the query)
String query=" select users.username, sum(evaluation.mark)\n" +
"from users inner JOIN user_has_eval \n" +
"on users.id= user_has_eval.user_id inner JOIN\n" +
"evaluation ON evaluation.id=user_has_eval.eval_id\n" +
"where evaluation.types=\"Secretaria\" and users.id = ? \n" +
"\n" ;
Trying to join 3 tables within a query returns an empty result. Strange enough, having one table removed (two tables join) returns some set. Here is what I do:
String sql = "SELECT\n" +
" tc.constraint_name, tc.table_name, kcu.column_name, \n" +
" ccu.table_name AS foreign_table_name,\n" +
" ccu.column_name AS foreign_column_name, constraint_type \n" +
"FROM \n" +
" information_schema.table_constraints AS tc \n" +
" JOIN information_schema.key_column_usage AS kcu\n" +
" ON tc.constraint_name = kcu.constraint_name\n" +
" JOIN information_schema.constraint_column_usage AS ccu\n" +
" ON ccu.constraint_name = tc.constraint_name\n" +
"WHERE constraint_type = 'FOREIGN KEY'";
List<Map<String, Object>> foreignTable1 = jdbcTemplate(getShardId(sku)).queryForList(sql);
Would always return an empty set.
Try using outer joins and check whether there are rows which don't have corresponding IDs so that the join removes the non-matching rows. Especially that you write, that two tables result in a non-empty result set seems to indicate, that the join with the third table does not result in matching rows of the result set of the first two.