Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql sql更新表条件取决于同一表中的其他行_Postgresql - Fatal编程技术网

Postgresql sql更新表条件取决于同一表中的其他行

Postgresql sql更新表条件取决于同一表中的其他行,postgresql,Postgresql,我有下表 id,link_id,url,type,download,filename 44,11,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg 45,11,https://google.com,extra,0,53bfa01d-91d0-4b84-9389-b06e5e4ef618.jpg 46,11,https://google.com,extra,0,364cfdc2-c0b6-43fc-8936-33

我有下表

id,link_id,url,type,download,filename
44,11,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
45,11,https://google.com,extra,0,53bfa01d-91d0-4b84-9389-b06e5e4ef618.jpg
46,11,https://google.com,extra,0,364cfdc2-c0b6-43fc-8936-33e49896014a.jpg
47,12,https://google.com,extra,0,9d26efbd-e6e0-42df-bde0-04c05babffe4.jpg
48,13,https://yahoo.com,extra,0,2d58b9f7-1860-40d8-88f0-9fc08cd7275f.jpg
49,13,https://yahoo.com,extra,0,574b1646-6316-4a4b-8e28-56c38c0999b9.jpg
...
我想写一个查询,检查download=1,并用相同的url和download=0更新所有行,以设置相同的文件名和download=1

因此,输出将是

44,11,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
45,11,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
46,11,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
47,12,https://google.com,extra,1,126cd08a-b963-48e5-878e-96dea057d57e.jpg
48,13,https://yahoo.com,extra,0,2d58b9f7-1860-40d8-88f0-9fc08cd7275f.jpg
49,13,https://yahoo,extra,0,574b1646-6316-4a4b-8e28-56c38c0999b9.jpg

  • 使用
    download=1
  • 查找所有相关的url记录
  • 使用步骤1中获取的数据更新列

  • 很好,一个问题
    SET download=s。download
    将始终等于1,对吗?是的,因为您在子查询中筛选了download=1
    UPDATE mytable t
    SET download = s.download, filename = s.filename   -- 3 
    FROM (
        SELECT url, download, filename                 -- 1
        FROM mytable
        WHERE download = 1
    ) s
    WHERE t.url = s.url                                -- 2