如何在python中将WITH as SELECT查询表达式转换为?

时间:2018-10-04 09:03:14

标签: python sqlalchemy

我有以下查询要从数据库表中删除重复项。

WITH x AS (SELECT "region_code" dup, min(ctid) 
FROM  public.test2 GROUP BY "region_code"  
HAVING count(*) > 1) 
DELETE FROM public.test2  
USING     x 
WHERE     (region_code) = (dup)  AND  public.test2.ctid <> x.min 
RETURNING *;

现在,我想使用python执行此查询。当我在python中运行此查询时,没有任何反应。我在python 3.6中使用sqlalchemy。

query = "WITH x AS (SELECT \"region_code\" dup, min(ctid) FROM  " + schema + "." + table_name + " GROUP BY \"region_code\" HAVING count(*) > 1) DELETE FROM " + schema + "." + table_name +" USING     x WHERE     (region_code) = (dup)  AND  " + schema + "." + table_name +".ctid <> x.min RETURNING *;"
data = con.execute(query)

0 个答案:

没有答案