gpt4 book ai didi

sql - MySQL 更新 - MAX() GROUP BY

转载 作者:行者123 更新时间:2023-11-29 09:17:51 25 4
gpt4 key购买 nike

如何更改:

SELECT user.postcode, max(postcode.postcode) as postcode
FROM user
INNER JOIN postcode ON user.postcode LIKE CONCAT( postcode.postcode, "%" )
GROUP BY user.postcode

进入类似的更新?

UPDATE user 
INNER JOIN postcode ON user.postcode LIKE CONCAT(postcode.postcode, "%")
SET user.lat = postcode.lat, user.lng = postcode.lng

我不知道如何考虑 MAX() 和 GROUP BY

回答

下面是我的@ceteras 代码版本。效果很好!

DROP TABLE IF EXISTS tmp;
CREATE TABLE tmp AS
SELECT user.user_id, max(postcode.postcode) AS postcode, postcode.lat, postcode.lng
FROM user
INNER JOIN postcode ON user.postcode LIKE CONCAT( postcode.postcode, "%" )
GROUP BY user.postcode;
ALTER TABLE tmp ADD PRIMARY KEY(user_id);

UPDATE user
INNER JOIN tmp ON user.user_id = tmp.user_id
SET user.lat1 = tmp.lat, user.lng1 = tmp.lng;

DROP TABLE tmp;

最佳答案

这是一份一次性工作吗?如果是,请尝试以下操作:

drop table if exists tmp;
create table tmp as
SELECT user.postcode, max(postcode.postcode) as post_code
FROM user
INNER JOIN postcode ON user.postcode LIKE CONCAT( postcode.postcode, "%" )
GROUP BY user.postcode;
alter table tmp add unique key(postcode, post_code), add key (post_code, postcode);

UPDATE user
INNER JOIN tmp ON user.postcode = tmp.postcode
inner join postcode p on p.postcode = tmp.post_code
SET user.lat = p.lat, user.lng = p.lng;

drop table tmp;

关于sql - MySQL 更新 - MAX() GROUP BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3493691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com