0
select 
                ID,NAME,SEX,RESIDENCE,EMAIL,MOBILE,PASSWORD,MOOD,BIRTHDAY,AVATAR,TYPE,MONEY
             from 
                user 
             where 
                ID in
                    (select USER2 as id from friend where USER1=#userId# and STATUS= 1 union select user1 as id from friend where user2 = #userId#  )



select 
                ID,NAME,SEX,RESIDENCE,EMAIL,MOBILE,PASSWORD,MOOD,BIRTHDAY,AVATAR,TYPE,MONEY
             from 
                user 
             where 
                ID in
                    (select USER2 as id from friend where USER1=#userId# and STATUS= 1)

如果我使用第二个 sql 没关系,如果我先使用它会运行错误。我认为参数没问题,因为两个 sql 具有相同的参数

org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];   
--- The error occurred in com/bit/hapin/dao/activity/activity.xml.  
--- The error occurred while applying a parameter map.  
--- Check the getInviteFriendForActivity-InlineParameterMap.  
--- Check the parameter mapping for the 'userId' property.  
--- Cause: java.lang.ArrayIndexOutOfBoundsException: 1; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in com/bit/hapin/dao/activity/activity.xml.  
--- The error occurred while applying a parameter map.  
--- Check the getInviteFriendForActivity-InlineParameterMap.  
--- Check the parameter mapping for the 'userId' property.  
--- Cause: java.lang.ArrayIndexOutOfBoundsException: 1
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.translate(SQLStateSQLExceptionTranslator.java:121)
    at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:322)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:212)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.executeWithListResult(SqlMapClientTemplate.java:249)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:296)
    at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(SqlMapClientTemplate.java:290)
    at com.bit.hapin.dao.activity.ActivityDAOImpl.getInviteFriendForActivity(ActivityDAOImpl.java:51)

所以我该怎么做?

4

1 回答 1

0

第一次查询没有问题。但,

在第一个查询中:

select USER2 as id from friend ..... select user1 as id from friend ....

这意味着friend表有USER2user1列。如果是这样,请确保表中的表列(USER2,user1)friend。虽然我不知道您的程序要求,但第二个查询(在您的帖子中)就足够了,因为您正在尝试union使用single/same表。

也可以参考:Union on the same table

于 2012-11-11T09:26:38.470 回答