I am trying to produce the below sql in sqlalchemy core. I am not able to get the parentheses in the conjunction AND OR to appear.
select from member t1
inner join member t2
on (
(
t1.first_name = t2.last_name and
t1.last_name = t2.first_name and
t1.dob = t2.dob
)
or (
t1.last_name = t2.last_name and
t1.first_name = t2.first_name and
t1.dob = t2.dob
)
)
group by t2.id
The sqlalchemy core statement that I am using is:
selStmt = select([t1]).select_from(
t1.join(
t2, or_(
and_(
t1.c.first_name == t2.c.last_name,
t1.c.last_name == t2.c.first_name,
t1.c.dob == t2.c.dob
),
and_(
t1.c.last_name == t2.c.last_name,
t1.c.first_name == t2.c.first_name,
t1.c.dob == t2.c.dob
)
)
)
).group_by(t2.c.id)
The resulting sql code is:
SELECT t1
FROM t1
JOIN t2 ON
t1.first_name = t2.last_name AND
t1.last_name = t2.first_name AND
t1.dob = t2.dob OR
t1.last_name = t2.last_name AND
t1.first_name = t2.first_name AND
t1.dob = t2.dob
GROUP BY t2.id
Since the parentheses are not included the logic is not correct. How do I get parentheses in conjunctions?
Apparently the AND operation has higher precedence over OR. Therefore in my case the parentheses are not required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With