Java/Ebean和或查询
我正在尝试执行sql查询,如下所示:Java/Ebean和或查询,java,playframework-2.0,ebean,Java,Playframework 2.0,Ebean,我正在尝试执行sql查询,如下所示: select * from conversations where (user_1 = user_1_id and user_2 = user_2_id ) or (user_1 = user_2_id and user_2 = user_1_id ) 我用Ebian写的,所以: Conversation conversation = Ebean.find(Conversation.class)
select * from conversations where (user_1 = user_1_id and user_2 = user_2_id ) or (user_1 = user_2_id and user_2 = user_1_id )
我用Ebian写的,所以:
Conversation conversation = Ebean.find(Conversation.class)
.where()
.and(Expr.eq("user_1", user_1_id), Expr.eq("user_2", user_2_id))
.or(Expr.eq("user_1", user_2_id), Expr.eq("user_2", user_1_id))
.findUnique();
但是,这会产生以下查询:
select * from conversation where (user_1 = user_1_id and user_2 = user_2_id ) and (user_1 = user_2_id or user_2 = user_1_id )
我一直在看文档,但对于如何优化查询以满足我的需要,我没有一个清晰的想法
有人能帮忙吗?看一下(它有一个or表达式作为示例):
结果:
(用户\ 1=用户\ 2 \ id或用户\ 2=用户\ 1 \ id)
可以说,您在第二个条件中使用or表达式,而不是在两个条件之间使用or表达式
您需要首先使用或方法:
Conversation conversation = Ebean.find(Conversation.class)
.where().or(
Expr.and(Expr.eq("user_1", user_1_id), Expr.eq("user_2", user_2_id)),
Expr.and(Expr.eq("user_1", user_2_id), Expr.eq("user_2", user_1_id)))
.findUnique();
这会使你的妄想症和内心的状态之间出现or。谢谢你的帮助!我曾经尝试过类似的东西,但它无法编译,所以我认为我走错了方向。不过这很管用,谢谢!
.or(Expr.eq("user_1", user_2_id), Expr.eq("user_2", user_1_id))
Conversation conversation = Ebean.find(Conversation.class)
.where().or(
Expr.and(Expr.eq("user_1", user_1_id), Expr.eq("user_2", user_2_id)),
Expr.and(Expr.eq("user_1", user_2_id), Expr.eq("user_2", user_1_id)))
.findUnique();