Postgres Complex Query to get column values having corresponding column joined with another table having a specific value
Ok i finally constructed the right query as follows:
UPDATE app.affiliations
SET current = true
where current = false
and user_id in (select r.user_id
from app.affiliations as a join app.relationships as r
on r.target_user_id = a.user_id
group by r.user_id
having false = ALL(array_agg(a.current))
)
Complex SQL query works in Postgresql pgAdmin 4 but not Python
Per Parameters:
import psycopg2
con = psycopg2.connect(dbname="test", host='localhost', user='postgres')
cur = con.cursor()
number = 1
#mogrify returns an adapted query string. Used here to show that the
#query is correctly built. Substitute execute for actual usage.
cur.mogrify("select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = %s and product.prodtempindex = %s",[number, 2])
'select product.width, product.height from product inner join product_template on product.prodtempindex = product_template.prodtempindex inner join painting on painting.pntindex = product.pntindex where painting.catalognumber = 1 and product.prodtempindex = 2'
How to query complex range in postgreSql
SELECT * FROM TABLE_NAME WHERE
(dateFrom <= Req.from AND dateTo >= Req.from)OR
(dateFrom <= Req.to AND dateTo >= Req.to) OR
(dateFrom >= Req.from AND dateTo <= Req.to)
I didn't find better then this
Related Topics
Ukrainian Character Change to Question Mark When Insert to Table
How to Generate a Random, Unique, Alphanumeric Id of Length N in Postgres 9.6+
Sqllite Strftime Not Reading Column Value
What Is Difference Between Inner Join and Outer Join
Update Xml Tag in a Clob Column in Oracle
What Is the Optimal Way to Compare Dates in Microsoft SQL Server
Query the Two Cities in Station with the Shortest and Longest City Names,
Getting the Sum of a Datediff Result
SQL Server 2008 Unique Column That Is Case Sensitive
How to Constraint No Empty Strings on an Nvarchar Column
Query SQL to Subtract Two Fields
Access 2007: "Select Count(Distinct ..."
Using 3 Updates in the Same Store Procedure? "Small Error"
Index for Multiple Columns in Activerecord
How to Use Merge on Linked Servers