我有5个表,每个表中的模式是(com_no,uid,date)。我想访问所有的表并获取相同的uid的com_no。我在写JDBC此查询作为访问结果集中的多个表
String s1= (String)session.getAttribute("uid");
rs1=stat.executeQuery("select distinct com_no from hostel,sports where uid='"+s1+"'");
while(rs1.next()) {
out.println(rs1.getString("com_no"));
}
但servlet异常是:
The specified field 'com_no' could refer to more than one table listed in the FROM clause of your SQL statement.
任何人可以帮助我imrove此查询或者给我一些其他的方式来做到这一点。任何帮助将不胜感激。
这不是一个servlet异常,它是SQL异常。 SQL本身是不正确的。查询数据库时,必须正确指定列。如果com_no仅在其中一个表中可用,那么它很好,否则会出错。在没有任何连接条件的情况下,在选择查询中指定两个表需要什么? –
@AhhijithNagarajan我想打印所有在同一个uid多个表中的com_no。所以我试图使用这个查询。你能以其他方式建议吗? – Ani
你正在使用哪个数据库?几乎所有的数据库,你都可以使用联合, 从table1中选择com_no,其中uid =? union 从table2中选择com_no,其中uid =? –