1. 程式人生 > >mysql將兩張表的兩個列連線後更新到第三張表中

mysql將兩張表的兩個列連線後更新到第三張表中

需求,現在有小區表community


單元樓表flat


房間表


現在要將community中小區地址和flat中門牌號拼接更新到room表中fullAddress中。

實現如下:

UPDATE room r
LEFT JOIN flat f ON f.id=r.flatID
LEFT JOIN community c ON c.id=f.communityID
SET r.fullAddress= CONCAT(c.address,f.num)

執行結果:


如果有條件,直接在後面新增where語句即可。

測試資料:

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50623
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 50623
File Encoding         : 65001

Date: 2016-06-11 08:10:40
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `community`
-- ----------------------------
DROP TABLE IF EXISTS `community`;
CREATE TABLE `community` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `address` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of community
-- ----------------------------
INSERT INTO `community` VALUES ('1', '西雅圖小區', '徐彙區三江路88弄');
INSERT INTO `community` VALUES ('2', '王家堂小區', '斜土路2561弄1-17號');
INSERT INTO `community` VALUES ('3', '東亞大樓', '斜土路2570號');
INSERT INTO `community` VALUES ('4', '四天小區', '中山南路918弄48-49號');
INSERT INTO `community` VALUES ('5', '田林二村', '田林路65弄510幢13-17單號');

-- ----------------------------
-- Table structure for `flat`
-- ----------------------------
DROP TABLE IF EXISTS `flat`;
CREATE TABLE `flat` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `communityID` varchar(30) DEFAULT NULL,
  `num` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of flat
-- ----------------------------
INSERT INTO `flat` VALUES ('1', '1', '3號1201室');
INSERT INTO `flat` VALUES ('2', '1', '3號1202室');
INSERT INTO `flat` VALUES ('3', '1', '3號1205室');
INSERT INTO `flat` VALUES ('4', '2', '5號501室');
INSERT INTO `flat` VALUES ('5', '2', '5號506室');
INSERT INTO `flat` VALUES ('6', '2', '5號504室');
INSERT INTO `flat` VALUES ('7', '3', '12號205室');
INSERT INTO `flat` VALUES ('8', '3', '12號102室');
INSERT INTO `flat` VALUES ('9', '3', '12號101室');

-- ----------------------------
-- Table structure for `results`
-- ----------------------------
DROP TABLE IF EXISTS `results`;
CREATE TABLE `results` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `year` int(11) DEFAULT NULL,
  `subject` int(255) DEFAULT NULL,
  `score` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of results
-- ----------------------------
INSERT INTO `results` VALUES ('1', '2001', '1', '43');
INSERT INTO `results` VALUES ('2', '2001', '2', '45');
INSERT INTO `results` VALUES ('3', '2001', '3', '54');
INSERT INTO `results` VALUES ('4', '2001', '4', '63');
INSERT INTO `results` VALUES ('5', '2002', '1', '34');
INSERT INTO `results` VALUES ('6', '2002', '2', '32');
INSERT INTO `results` VALUES ('7', '2002', '3', '23');
INSERT INTO `results` VALUES ('8', '2002', '4', '54');

-- ----------------------------
-- Table structure for `room`
-- ----------------------------
DROP TABLE IF EXISTS `room`;
CREATE TABLE `room` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `flatID` int(11) DEFAULT NULL,
  `fullAddress` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of room
-- ----------------------------
INSERT INTO `room` VALUES ('1', '1', null);
INSERT INTO `room` VALUES ('2', '2', null);
INSERT INTO `room` VALUES ('3', '3', null);
INSERT INTO `room` VALUES ('4', '4', null);
INSERT INTO `room` VALUES ('5', '5', null);
INSERT INTO `room` VALUES ('6', '6', null);
INSERT INTO `room` VALUES ('7', '7', null);
INSERT INTO `room` VALUES ('8', '8', null);
INSERT INTO `room` VALUES ('9', '9', null);
INSERT INTO `room` VALUES ('10', '10', null);