0

我有 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.

任何人都可以帮助我改进这个查询或给我一些其他的方法来做到这一点。任何帮助将不胜感激。

4

2 回答 2

0

使用UNIONor和别名来避免查询JOIN中不明显的字段。SELECT

首先,我想知道为什么您有 5 个表的方案完全相同?

于 2013-10-18T16:33:06.590 回答
0

请通过检索数据指定您的需求,但您可以尝试以下代码以避免您现在遇到的错误,

   rs1=stat.executeQuery("select distinct hostel.com_no as hostelcom, sports.com_no as sportscom from hostel join sports on hostel.uid=sports.uid where hostel.uid='"+s1+"'");

  while(rs1.next()) {
  out.println(rs1.getString("hostelcom"));
   }

您必须查看 SQL Join 查询。

于 2013-10-18T16:31:52.633 回答