简体   繁体   English

sql其中x = y和x = z以及

[英]sql where x = y and x = z and

@English isn't my first language. @英语不是我的母语。

I have a code (it's working) : 我有一个代码(正在运行):

@Query("select new java.entity.dto.ProductsDTO(" +
        "p," +
        "count(c)," +
        "sum(case when c.user = :user then 1 else 0 end) > 0," +
        "sum(case when cart.user = :user then 1 else 0 end) > 0," +
        "sum(case when wish.user = :user then 1 else 0 end) > 0) " +
        "from Products p " +
        "left join p.commentaries c " +
        "left join p.carts cart " +
        "left join p.wishLists wish " +
        "left join p.tags tags " +
        "where p.categories = :categories " +
        "and tags.id in :tags " +
        "group by p,:user")
Page<ProductsDTO> findAllByCategoriesAndTags(Pageable pageable, @Param("categories") Categories categories, @Param("user") User user, @Param("tags") List<Long> tags);

As I read this part "tags.id = :tags " working like : "id = tag1 or id = tag2 or id = tag3 ...". 当我阅读此部分时,“ tags.id =:tags”的工作方式类似于:“ id = tag1或id = tag2或id = tag3 ...”。

So, how can I write somt thing like this : "id1 = tag1 or id1 = tag2 and id2 = tag1 or id2 = tag2" 所以,我该怎么写这样的东西:“ id1 = tag1或id1 = tag2和id2 = tag1或id2 = tag2”

As I read this part "tags.id = :tags " working like : " id = tag1 or id = tag2 or id = tag3 ...". 当我阅读此部分时,“ tags.id =:tags”的工作方式类似于:“ id = tag1或id = tag2或id = tag3 ...”。

So, how can I write somt thing like this : " id1 = tag1 or id1 = tag2 and id2 = tag1 or id2 = tag2 " 因此,如何编写类似这样的somt:“ id1 = tag1或id1 = tag2 id2 = tag1或id2 = tag2

Simply append another AND statement after the already existing one : 只需在已经存在的语句之后追加另一个AND语句:

    AND tags.id1 in :tags 
    AND tags.id2 in :tags

I assumed you meant " (id1 = tag1 or id1 = tag2) and (id2 = tag1 or id2 = tag2) ". 我假设您的意思是“ (id1 = tag1 or id1 = tag2) and (id2 = tag1 or id2 = tag2) ”。 Be careful, though, about the fact that AND logical operations take precedence over OR ones. 但是,请注意AND逻辑运算优先于OR运算这一事实。 Without brackets, your expression would have been parsed as " id1 = tag1 or (id1 = tag2 _and_ id2 = tag1) or id2 = tag2 "… 如果没有括号,您的表达式将被解析为“ id1 = tag1 or (id1 = tag2 _and_ id2 = tag1) or id2 = tag2 “…

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM