我需要更新某些表的记录,并且尝试使用以下命令:
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
表中,有以下列:
auc_final_price
bidding_price
拍卖ID
有几条记录,但列的记录相同: auction_id 和 auctionID 。
我需要的只是获取bid_account
表中输入的LAST值,然后将值复制到auction
表中。
答案 0 :(得分:2)
也许有一种更好的措辞来表达您的更新,但是,如果您要使用相关的子查询,则可能需要将它们直接放入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);
子句中:
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;
实际上,我认为我们仍然可以使用更新联接语法编写您的查询。由于您的表很大,因此您可以尝试加入一个临时表(代替一个有关系的视图):
qa03
答案 1 :(得分:2)
您的主要问题是您在联接中缺少“ 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);