使用现有数据更新命令

r1zhe5dt  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(292)

我需要更新一些表的记录,我尝试使用以下命令:

UPDATE auction a        
JOIN (SELECT bidding_price, user_id, auction_id FROM bid_account WHERE auction_id = a.auctionID ORDER BY bidpack_buy_date DESC LIMIT 1) ult_lance_a
SET a.auc_final_price=ult_lance_a.bidding_price, a.buy_user=ult_lance_a.user_id
WHERE a.auctionID=ult_lance_a.auction_id;

但是,我得到以下错误:


# 1054 - Column 'a.auctionID' unknown in 'where clause'

我做错了什么?
以下是命令的返回: SELECT bidding_price, user_id, auction_id FROM bid_account WHERE auction_id = XXX ORDER BY bidpack_buy_date DESC LIMIT 1 投标价格=6.23
用户id=1720
拍卖id=1818
auction 表中,有以下列:
最终拍卖价格
投标价格
拍卖ID
有多个记录,但列的记录是相同的:auction\u id和auctionid。
我需要的是获取 bid_account 表,并将值复制到 auction table。

wfveoks0

wfveoks01#

可能有更好的方法来表达您的更新,但是如果您想使用相关的子查询,那么您可能需要将它们直接放到 SET 条款:

UPDATE auction a
SET auc_final_price = (SELECT bidding_price
                       FROM bid_account
                       WHERE auction_id = a.auctionID
                       ORDER BY bidpack_buy_date DESC
                       LIMIT 1),
    buy_user = (SELECT user_id
                FROM bid_account
                WHERE auction_id = a.auctionID
                ORDER BY bidpack_buy_date DESC
                LIMIT 1);

实际上,我认为我们仍然可以使用updatejoin语法编写查询。由于表很大,您可以尝试连接到临时表(代替匹配视图):

CREATE TEMPORARY TABLE IF NOT EXISTS 
    auctionView ( INDEX(auction_id) ) 
    ENGINE=MyISAM 
AS (
    SELECT b1.auction_id, b1.bidding_price, b1.user_id
    FROM bid_account b1
    INNER JOIN
    (
        SELECT auction_id, MAX(bidpack_buy_date) AS max_buy_date
        FROM bid_account
        GROUP BY auction_id
    ) b2
        ON b1.auction_id = b2.auction_id AND b1.bidpack_buy_date = b2.max_buy_date
);

UPDATE auction a
INNER JOIN auctionView b
    ON b.auction_id = a.auctionID
SET
    a.auc_final_price = b.bidding_price,
    a.buy_user = b.user_id;
aiazj4mn

aiazj4mn2#

您的主要问题是在join中缺少“on”子句,并且试图使用子查询的“where”子句完成相同的任务。
a、 auctionid在子查询的上下文中不存在 SELECT bidding_price, user_id, auction_id FROM bid_account WHERE auction_id = a.auctionID ... ,所以它抛出了一个错误。
不如这样试试:

UPDATE 
    auction AS a
INNER JOIN 
    /* Find the highest (max) bidpack_buy_date for each auction */
    (SELECT MAX(bidpack_buy_date) AS `Max Date`, auction_id FROM bid_account GROUP BY auction_id) AS maxdates
    ON 
    maxdates.auction_id = a.auctionID
INNER JOIN
    /* The data from bid_acount that accompanies the max dates found above */
    bid_account AS ult_lance_a
    ON
    ult_lance_a.bidpack_buy_date = maxdates.`Max Date`
    AND
    ult_lance_a.auction_id = maxdates.auction_ID
SET 
    a.auc_final_price = ult_lance_a.bidding_price,
    a.buy_user = ult_lance_a.user_id

我用来设置测试的代码:

DROP TABLE IF EXISTS bid_account;
CREATE TABLE IF NOT EXISTS bid_account (
    auction_id int,
    user_id int, 
    bidding_price numeric(8,2),
    bidpack_buy_date DATETIME
);

DROP TABLE IF EXISTS auction;
CREATE TABLE IF NOT EXISTS auction (
    auctionID int,
    buy_user int,
    auc_final_price numeric(8,2)
);

INSERT INTO 
    bid_account 
        (auction_id, user_id, bidding_price, bidpack_buy_date)
    VALUES
        (1,1, 10.00, '2017-01-01 12:00:01 PM'),
        (1,2, 20.00, '2017-01-01 12:00:02 PM'),
        (1,3, 30.00, '2017-01-01 12:00:03 PM'),
        (1,4, 40.00, '2017-01-01 12:00:04 PM'),
        (2,1, 10.00, '2017-01-01 12:00:01 PM'),
        (2,2, 20.00, '2017-01-01 12:00:02 PM'),
        (2,3, 30.00, '2017-01-01 12:00:03 PM');

INSERT INTO 
    auction 
        (auctionID)
    VALUES
        (1),
        (2),
        (3),
        (4),
        (5),
        (6),
        (7);

相关问题