0
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相似?
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;
你为什么使用'LIKE CONCAT(postcode.postcode,“%”)'? – wimvds 2010-08-16 14:31:28