-
-
Save timowest/5098112 to your computer and use it in GitHub Desktop.
#1 | |
e1.or(e2).and(e3)) | |
creates at first an 'or' operation based on e1 and e2 and uses this 'or' operation | |
in a top level 'and' operation with e3 as the second operand. | |
Since 'and' has higher precedence than 'or' it is serialized as | |
(e1 or e2) and e3 | |
#2 | |
e1.or(e2).and(e3.or(e4))) | |
'and' is the top level operation and e1.or(e2) and e3.or(e4) are the lower level operations | |
It is serialized as | |
(e1 or e2) and (e3 or e4) | |
#3 | |
e1.and(e2).or(e3.and(a4)) | |
`or`is the top level operation and e1.and(e2) and e3.and(e4) are the lower level operations | |
It is serialized as | |
e1 and e2 or e3 and e4 |
@ttwd80 Just do e1.and(e2).or(e3.and(e4)). First it groups e1.and(e2) and result is used as "left side" in OR (e3.and(e4)) which uses parenthesis.
@kerrermanisNL Yes, you assume right.
Ad original "Since 'and' has higher precedence than 'or' it is serialized as"... I'd not say that AND is of higher precedence, it is higher in a tree (if drawn as typical and not as trees in nature :-)). I guess precedence has nothing to do with it as your #3 example shows. First and is still lower in the tree.
Simply put: any further chained operator (as in: e1.op(e2).op(e3)...) is higher and higher in the tree (going from left) and if we want to nest something under it on the right side, we need to put the whole subexpression into a parenthesis.
More nesting - to get: ((e1 or e2) and (e3 or e4)) or (e5 and e6)
We write:
e1.or(e2).and(e3.or(e4)).or(e5.and(e6))
^under-^ ^--under-----^^-tree-root
How can I create
(e1 and e2) or (e3 and e4) or (e5 and e6) ?
@b0c1 like it is said in the example #3, you need to do : e1.and(e2).or(e3.and(e4)).or(e5.and(e6))
It will be serialized : e1 and e2 or e3 and e4 or e5 and e6
and because in SQL, and have precedence over or, it will be evaluated as (e1 and e2) or (e3 and e4) or (e5 and e6)
@iheb719
Thank you for teaching me operator priority
How can I create?
((e1 or e2) and e3) or (e4 and e5 and e6)
sql examples:
and (
(
(t3.id is null or t3.is_deleted = 1)
and t2.settle_cycle_id is NULL
and t1.settle_date BETWEEN '2022-04-01' and '2022-04-22'
)
or
(
t3.id is not null
and t3.is_deleted = 0
and t1.settle_date BETWEEN '2022-04-01' and '2022-04-22'
)
)
As I understand, if I want to get e1 and e2 and ((e3 and e4) or e5), it will be e1.and(e2).and(e3.and(e4).or(e5)).
But actually, I got e1 and e2 and (e3 and e4 or e5).
What is the matter? Please help me...
@SeongEon-Jo That's OK. AND has higher priority than OR, so "e1 and e2 and (e3 and e4 or e5)" means that "e3 and e4" will be evaluated first, then "... or e5" and then it's all AND-ed. Just the redundant parentheses are removed. It will work the same.
@virgo47 Oh, I got it. Thanks!
#3 works as explained.
what if i want something like (e1 AND e2) or (e3 and e4).