使用 Inner Join 进行更新的 MySQL 查询
用户最近在 MySQL 中尝试使用 INNER JOIN 执行 UPDATE 语句时遇到错误5.0。尽管使用了正确的字段名称,还是发生了错误。
原始查询如下:
UPDATE b SET b.mapx = g.latitude, b.mapy = g.longitude FROM business AS b INNER JOIN business_geocode g ON b.business_id = g.business_id WHERE (b.mapx = '' OR b.mapx = 0) AND g.latitude > 0
问题在于查询的语法。要解决此问题,必须在 UPDATE 语句中显式命名表。更正后的查询为:
UPDATE business AS b INNER JOIN business_geocode AS g ON b.business_id = g.business_id SET b.mapx = g.latitude, b.mapy = g.longitude WHERE (b.mapx = '' or b.mapx = 0) and g.latitude > 0
用户还遇到了语法错误。为了确认更正后的查询不包含语法错误,创建了以下表进行测试:
CREATE TABLE business (business_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, mapx VARCHAR(255), mapy VARCHAR(255)) ENGINE=INNODB; CREATE TABLE business_geocode (business_geocode_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, business_id INT UNSIGNED NOT NULL, latitude VARCHAR(255) NOT NULL, longitude VARCHAR(255) NOT NULL, FOREIGN KEY (business_id) REFERENCES business(business_id)) ENGINE=INNODB;
使用 MySQL 5.5.8 对这些表执行更正后的查询时,没有发生语法错误。查询结果显示没有匹配或更改的行。
以上是如何使用INNER JOIN正确更新MySQL数据?的详细内容。更多信息请关注PHP中文网其他相关文章!