簡體   English   中英

通過在列中插入相同的值來“插入SQL”

[英]“Insert SQL” by inserting the same value into the column

我有一個出價系統,可以與自動出價系統配合使用,還可以與客戶可以出價的按鈕一起使用。

客戶端單擊該按鈕時,它將調用一個函數,該函數執行以下查詢:

$qryins = "Insert into bid_account (user_id,bidpack_buy_date,bid_count,auction_id,product_id,bid_flag,bidding_price,bidding_type,bidding_time)
                values('$uid',NOW(),'1','$aid','$prid','d',$newprice,'s'," . $oldtime . ")";

該查詢在PHP文件中。

機械手的一部分由數據庫中的過程完成,並使用以下命令,該命令每秒運行一次:

Insert into bid_account (user_id, bidpack_buy_date, bid_count, auction_id, product_id, bid_flag, bidding_price, bidding_type, bidding_time)
        SELECT prox_user_id, NOW(), '1', auctionID, productID, 'd', prox_valor, 's', auc_due_time FROM t_autolances
        WHERE prox_valor NOT IN (SELECT bidding_price FROM bid_account WHERE auction_id=auctionID); 

競標有一個計時器,等於這些美分的拍賣。 出現的問題是,如果客戶在機器人給出出價的同時單擊按鈕,它將以相等的值寫入數據庫。

我需要它將不同的值寫入“ bidding_price ”列,如下所示:

我需要這樣寫:

robot 0.25
customer 0.26

而不是這樣:

robot 0.25
client 0.25

我不想將所有代碼都放在這里,因為它太長了,所以我將最重要的部分放在這里。

$q = "select * from auc_due_table adt left join auction a on adt.auction_id=a.auctionID left join auction_management am on a.time_duration=am.auc_manage where auction_id=$aid";
$r = mysqli_query($db, $q);
$ob = mysqli_fetch_object($r);

$oldtime = $ob->auc_due_time;
$oldprice = $ob->auc_due_price;
$plusprice = $ob->auc_plus_price;
$plustime = $ob->auc_plus_time;
$newprice = $ob->pennyauction == 1 ? $oldprice + 0.01 : $oldprice + $plusprice;
$newtime = $oldtime < $plustime ? $plustime : $oldtime;

也許這:

   Insert 
       into bid_account 
                     (
                     user_id, 
                     bidpack_buy_date, 
                     bid_count, 
                     auction_id, 
                     product_id, 
                     bid_flag, 
                     bidding_price, 
                     bidding_type, 
                     bidding_time
                     )
             SELECT 
                     prox_user_id, 
                     NOW(), 
                     '1', 
                     auctionID, 
                     productID, 
                     'd', 
                     (SELECT max(bidding_price)+0.01 FROM bid_account WHERE auction_id=auctionID) AS bidding_price,                     
                     's', 
                     auc_due_time 
             FROM 
                     t_autolances
             LIMIT 1

您需要在SQL中而不是在PHP中添加用戶數量。

$qryins = "Insert into bid_account (user_id,bidpack_buy_date,bid_count,auction_id,product_id,bid_flag,bidding_price,bidding_type,bidding_time)
            SELECT '$uid', NOW(),'1', auction_id, '$prid','d', 
                    auc_due_price + IF(pennyauction, 0.01, auc_plus_price), 's', auc_due_time
            from auc_due_table adt 
            left join auction a on adt.auction_id=a.auctionID 
            left join auction_management am on a.time_duration=am.auc_manage 
            where auction_id=$aid";

另一種解決方案是使用事務。 SELECT查詢之前啟動事務,並在INSERT之后提交事務。

mysqli_query($db, "START TRANSACTION");
$r = mysqli_query($db, $q);
$ob = mysqli_fetch_object($r);
$oldtime = $ob->auc_due_time;
$oldprice = $ob->auc_due_price;
$plusprice = $ob->auc_plus_price;
$plustime = $ob->auc_plus_time;
$newprice = $ob->pennyauction == 1 ? $oldprice + 0.01 : $oldprice + $plusprice;
$newtime = $oldtime < $plustime ? $plustime : $oldtime;
$qryins = "...";
mysqli_query($db, $qryins);
mysqli_query($db, "COMMIT");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM