第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

顯示兩個(gè)價(jià)目表之間的差異

顯示兩個(gè)價(jià)目表之間的差異

PHP
青春有我 2023-06-24 15:55:59
我有產(chǎn)品價(jià)格的歷史列表(按created_at排序),如下所示:Array(    [0] => stdClass Object        (            [id] => 1            [product_id] => 49            [price] => 14520000.0000            [created_at] => 1592501154        )    [1] => stdClass Object        (            [id] => 2            [product_id] => 49            [price] => 14000000.0000            [created_at] => 1592587554        )    [2] => stdClass Object        (            [id] => 4            [product_id] => 49            [price] => 14800000.0000            [created_at] => 1592673954        )    [3] => stdClass Object        (            [id] => 5            [product_id] => 49            [price] => 10000000.0000            [created_at] => 1592760354        )    [4] => stdClass Object        (            [id] => 6            [product_id] => 49            [price] => 14000000.0000            [created_at] => 1592846754        )    [5] => stdClass Object        (            [id] => 7            [product_id] => 49            [price] => 14000000.0000            [created_at] => 1592933154        )    [6] => stdClass Object        (            [id] => 8            [product_id] => 49            [price] => 14000000.0000            [created_at] => 1593019554        ))現(xiàn)在,為了在表中顯示數(shù)據(jù),我使用foreach如下方法列出了價(jià)格:   <?php foreach($product_prices_list as $product_price_list):?>     <tr>         <td><?= esc($product_price_list->created_at);?></td>         <td class="text-center"><?= esc(number_format($product_price_list->price));?></td>         <td class="text-center"></td> //show difference between of two price     </tr>   <?php endforeach;?>我可以在表中看到真實(shí)的輸出,但我需要在第三列中顯示兩個(gè)價(jià)格之間的差異,如下圖所示:我如何顯示列表中兩個(gè)價(jià)格之間的差異?!
查看完整描述

2 回答

?
ITMISS

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊

您只需將當(dāng)前價(jià)格屬性與數(shù)組中前一個(gè)對(duì)象的價(jià)格進(jìn)行比較?


像這樣的東西應(yīng)該有效:


<?php foreach($product_prices_list as $key => $product_price_list):?>

    <tr>

        <td><?= esc($product_price_list->created_at);?></td>

        <td class="text-center"><?= esc(number_format($product_price_list->price));?></td>

        <td class="text-center"><?= (!empty($product_prices_list[$key - 1])) ? $product_prices_list[$key + 1]->price - $product_price_list->price: 0; ?></td> //show difference between of two price

    </tr>

<?php endforeach;?>


查看完整回答
反對(duì) 回復(fù) 2023-06-24
?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果您運(yùn)行的是 MySQL 8.0,則可以使用窗口函數(shù)直接在數(shù)據(jù)庫(kù)中計(jì)算此信息:


select

    t.*,

    price 

        - lag(price, 1, price) over(partition by product_id order by created_at) 

        as price_diff

from mytable t

這會(huì)向結(jié)果集中再添加一列,其中包含同一產(chǎn)品的當(dāng)前價(jià)格與之前價(jià)格之間的差異。


查看完整回答
反對(duì) 回復(fù) 2023-06-24
  • 2 回答
  • 0 關(guān)注
  • 194 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)