sql update中的子查询可以获取刚刚更新的行吗?

8wtpewkr  于 2021-07-27  发布在  Java
关注(0)|答案(0)|浏览(345)

有一个出版物的集合,我想为每个作者分配一个不同的发布日期。为了做到这一点,我减去所有的日期,从出版日期到昨天,已经采取的日期为作者。
这个更新的问题是当前记录依赖于前一个记录的分配。例句:如果已经有一个功能分配给4月2日,那一天的新功能将被推到3日或以后。但是如果4月2日有两个未分配的特征,它们将被分配到同一天。

UPDATE publications pub
SET pub.release_date = (
    SELECT all.Dates 
    FROM ( # This generates all dates between publication date until yesterday
        SELECT curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) ) DAY as Dates
        FROM (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) as a
        CROSS JOIN (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) as b
        CROSS JOIN (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) as c
        CROSS JOIN (SELECT 0 as a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) as d
    ) all
    WHERE all.Dates > DATE(pub.date)
    AND all.Dates < curdate()
    AND all.Dates NOT IN ( # Already taken dates for this author
        SELECT DISTINCT(DATE(taken.release_date))
        FROM (SELECT * FROM publications) as taken
        WHERE taken.author_id = pub.author_id
        AND taken.release_date IS NOT NULL
        )
    ORDER BY Date ASC
    limit 1
)
WHERE pub.release_date is null
AND pub.type = 'feature';

我读到sql的工作方式(这里简化)是将数据集提取到缓冲区,修改它,然后存储。我猜mysql也做了类似的事情。这种不匹配似乎是因为子查询不是从我们正在更新的数据缓冲区读取数据,而是从原始数据集读取数据。
mysql不允许postgresql更新语法:

UPDATE ...
SET ...
FROM <- 
WHERE ...;

sql update中的子查询能否获取刚刚更新的行?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题