亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用相同的列值更新數據庫的最新行

使用相同的列值更新數據庫的最新行

慕斯709654 2022-12-06 16:40:31
我有一個如下表:------------------------------------------------------| year     |  period     |  publish_date |  status   |-----------|-------------|---------------|-----------||  2020    |     03      |  datetime_obj |     0     ||  2020    |     03      |  ...          |     0     ||  2020    |     03      |  ...          |     0     ||  2020    |     03      |  ...          |     0     ||  2020    |     04      |  ...          |     0     ||  2020    |     04      |  ...          |     0     |------------------------------------------------------我想更新任何年份和時期組的最新發布日期的狀態,但我對此一無所知例如:選擇周期為 03 的 2020 年的所有行,然后將最新的 publish_date 的狀態更新為 1,然后對周期為 04 的 2020 年執行相同的操作直到結束......我想在 django 中使用它所以請用 pythonic 方式解釋或者sql查詢這是我的 Django 模型:class Balance_Sheet(models.Model):    ...    publish_date = models.DateTimeField(auto_now=True)     year = models.IntegerField(null=True)    period = models.IntegerField(null=True)     status = models.IntegerField(default=0)      ...太感謝了
查看完整描述

3 回答

?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

解決方案 1


一個簡單的方法是迭代年/月應用過濾器:


years = {sheet.year for sheet in Balance_Sheet.objects.all()}


for year in years:

  year_months = {sheet.month for sheet in Balance_Sheet.objects.filter(year=year)}


  for month in year_months:

    object_to_update = Balance_Sheet.objects.filter(

      year=year,

      period=month

    ).order_by('-publish_date').first()


    object_to_update.status = 1

    object_to_update.save()

可能有更復雜的 ORM 函數來將其變成單個查詢,但我在那里編寫的代碼(未測試)將在大多數/所有 Django 版本中工作。此外,由于我們談論的是年/月數量,因此對數據庫的查詢量不會很大。


方案二


在 postgres 數據庫中,你可以試試這個:


Balance_Sheet.objects.all().order_by(

    'year', 'period', '-publish_date'

).distinct(

    'year', 'period'

).update(

    status=1

)

請注意,這不適用于其他數據庫,例如 MySQL。請在此處查看 django 文檔。



查看完整回答
反對 回復 2022-12-06
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

這在 mysql 中工作


UPDATE Balance_Sheet b

    INNER JOIN (SELECT max(publish_date) LastDate, year, period FROM bs GROUP BY year, period) C

    ON C.LastDate = b.publish_date and C.period = b.period and C.year = b.year

SET b.status = 1

where C.LastDate = b.publish_date and C.period = b.period and C.year = b.year

謝謝大家


查看完整回答
反對 回復 2022-12-06
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

我不熟悉 django 但它的 sql 看起來像這樣。由于您已經有了資產負債表。您只需要更新部分。


WITH balance_sheet(id ,yearmonth , publish_date, status)

AS (SELECT 1, '202003',  '2020-03-10' , 0   UNION 

    SELECT 2, '202003',  '2020-03-15' , 0   UNION

    SELECT 3, '202004',  '2020-04-20' , 0   UNION

    SELECT 4, '202004',  '2020-04-25' , 0

   )


UPDATE b

SET status = 1

FROM balance_sheet b

JOIN (Select *,RANK() OVER (partition by yearmonth ORDER BY publish_date desc) AS rnk 

from balance_sheet bal) b_latest ON b.id = b_latest.id

WHERE b_latest.rnk = 1


查看完整回答
反對 回復 2022-12-06
  • 3 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號