2016-08-08 195 views
0

鑑於t.id,a.id,t1.namet2.name,如何添加或更新t1_has_t2.data用INNER JOIN插入重複鍵

如果當前有記錄,我可以更新它。

UPDATE t1_has_t2 
INNER JOIN t1 ON t1.id=t1_has_t2.t1_id 
INNER JOIN t2 ON t2.id=t1_has_t2.t2_id 
SET t1_has_t2.data=123 
WHERE t1.name="foo" AND t1.t_id=333 AND t2.name="bar" AND t2.t_id=333; 

如果當前不存在記錄,我該如何插入它?

編輯。它會是類似於以下的東西嗎?在JOIN中看起來浪費了t

INSERT INTO t1_has_t2(t1_id,t2_id,data) 
SELECT t1.id, t2.id, 123 
FROM t 
INNER JOIN t1 ON t1.t_id=t.id 
INNER JOIN t2 ON t2.t_id=t.id 
WHERE t1.name="foo" AND t1.t_id=333 AND t2.name="bar" AND t2.t_id=333 
ON DUPLICATE KEY SET t1_has_t2.data=123; 

EDIT2。啊,也許我現在明白了。我剛剛通過他們的共享t.id加入t1和t2彼此?

INSERT INTO t1_has_t2(t1_id,t2_id,data) 
SELECT t1.id, t2.id, 123 
FROM t1 
INNER JOIN t2 ON t2.t_id=t1.t_id 
WHERE t1.name="foo" AND t1.t_id=333 AND t2.name="bar" AND t2.t_id=333 
ON DUPLICATE KEY UPDATE t1_has_t2.data=123; 

enter image description here

-- MySQL Script generated by MySQL Workbench 
-- 08/08/16 07:40:04 
-- Model: New Model Version: 1.0 
SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0; 
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; 
SET @[email protected]@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; 

-- ----------------------------------------------------- 
-- Schema mydb 
-- ----------------------------------------------------- 
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ; 
USE `mydb` ; 

-- ----------------------------------------------------- 
-- Table `mydb`.`accounts` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `mydb`.`accounts` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`id`)) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `mydb`.`t` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `mydb`.`t` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    `accounts_id` INT NOT NULL, 
    PRIMARY KEY (`id`, `accounts_id`), 
    INDEX `fk_t_accounts_idx` (`accounts_id` ASC), 
    CONSTRAINT `fk_t_accounts` 
    FOREIGN KEY (`accounts_id`) 
    REFERENCES `mydb`.`accounts` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `mydb`.`t1` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `mydb`.`t1` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    `t_id` INT NOT NULL, 
    `t_accounts_id` INT NOT NULL, 
    `name` VARCHAR(45) NULL, 
    PRIMARY KEY (`id`), 
    INDEX `fk_t1_t1_idx` (`t_id` ASC, `t_accounts_id` ASC), 
    UNIQUE INDEX `un1` (`t_id` ASC, `name` ASC), 
    CONSTRAINT `fk_t1_t1` 
    FOREIGN KEY (`t_id` , `t_accounts_id`) 
    REFERENCES `mydb`.`t` (`id` , `accounts_id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `mydb`.`t2` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `mydb`.`t2` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    `t_id` INT NOT NULL, 
    `t_accounts_id` INT NOT NULL, 
    `name` VARCHAR(45) NULL, 
    PRIMARY KEY (`id`), 
    INDEX `fk_t2_t1_idx` (`t_id` ASC, `t_accounts_id` ASC), 
    UNIQUE INDEX `un2` (`t_id` ASC, `name` ASC), 
    CONSTRAINT `fk_t2_t1` 
    FOREIGN KEY (`t_id` , `t_accounts_id`) 
    REFERENCES `mydb`.`t` (`id` , `accounts_id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


-- ----------------------------------------------------- 
-- Table `mydb`.`t1_has_t2` 
-- ----------------------------------------------------- 
CREATE TABLE IF NOT EXISTS `mydb`.`t1_has_t2` (
    `t1_id` INT NOT NULL, 
    `t2_id` INT NOT NULL, 
    `data` VARCHAR(45) NULL, 
    PRIMARY KEY (`t1_id`, `t2_id`), 
    INDEX `fk_t1_has_t2_t21_idx` (`t2_id` ASC), 
    INDEX `fk_t1_has_t2_t11_idx` (`t1_id` ASC), 
    CONSTRAINT `fk_t1_has_t2_t11` 
    FOREIGN KEY (`t1_id`) 
    REFERENCES `mydb`.`t1` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION, 
    CONSTRAINT `fk_t1_has_t2_t21` 
    FOREIGN KEY (`t2_id`) 
    REFERENCES `mydb`.`t2` (`id`) 
    ON DELETE NO ACTION 
    ON UPDATE NO ACTION) 
ENGINE = InnoDB; 


SET [email protected]_SQL_MODE; 
SET [email protected]_FOREIGN_KEY_CHECKS; 
SET [email protected]_UNIQUE_CHECKS; 
+0

不妨在數據庫管理員流中提問http://dba.stackexchange.com/ – alsobubbly

+0

@alsobubbly不知道在哪裏發佈這樣的問題。這兩個網站似乎都適合 – user1032531

+0

沒有問題,但認爲你可能會得到更好的迴應,雖然 – alsobubbly

回答

0

工作正常,如果我知道你想與Insert on Duplicate Key Update(IODKU)是什麼。

數據加載:

insert accounts(id) values (NULL); -- id = 1 
insert t(accounts_id) values (1); -- id = 1 

insert t1(t_id,t_accounts_id,name) values (1,1,'n1'); -- id=1 
insert t1(t_id,t_accounts_id,name) values (1,1,'n2'); -- id=2 
insert t2(t_id,t_accounts_id,name) values (1,1,'n1'); -- id=1 
insert t2(t_id,t_accounts_id,name) values (1,1,'n2'); -- id=2 

insert t1_has_t2(t1_id,t2_id,data) values(1,1,'one_one'); -- success 
insert t1_has_t2(t1_id,t2_id,data) values(1,77,'x'); -- Error 1452 as expected 
insert t1_has_t2(t1_id,t2_id,data) values(77,1,'x'); -- Error 1452 as expected 
insert t1_has_t2(t1_id,t2_id,data) values(1,2,'one_two'); -- success 
insert t1_has_t2(t1_id,t2_id,data) values(2,1,'two_one'); -- success 
insert t1_has_t2(t1_id,t2_id,data) values(2,2,'two_two'); -- success 

您的疑問:

UPDATE t1_has_t2 
INNER JOIN t1 ON t1.id=t1_has_t2.t1_id 
INNER JOIN t2 ON t2.id=t1_has_t2.t2_id 
SET t1_has_t2.data='I am a string' 
WHERE t1.name="n1" AND t1.t_id=1 AND t2.name="n1" AND t2.t_id=1; 

IODKU:

insert t1_has_t2(t1_id,t2_id,data) values(2,2,'two_two_version002') 
on duplicate key update data='anchovies'; 

見結果:

select * from t1_has_t2; 
+-------+-------+---------------+ 
| t1_id | t2_id | data   | 
+-------+-------+---------------+ 
|  1 |  1 | I am a string | 
|  1 |  2 | one_two  | 
|  2 |  1 | two_one  | 
|  2 |  2 | anchovies  | 
+-------+-------+---------------+ 

您也可以看看

insert ignore t1_has_t2(t1_id,t2_id,data) [something]; 

設計成功或失敗的靜默。

+0

爲你的IODKU查詢在這裏問沒有害處,但你沒有t1_id和t2_id的值,除非你做了一些先前的查詢,我不希望這樣做。 – user1032531

+0

如何繪製出你想要插入什麼,以及你想要更新什麼(基於什麼條件)。除非你說你想要1行進入,否則你對INSERT方面肯定沒有這樣做? – Drew

+0

我想我的EDIT2查詢工作。給定333的't.id','foo'的't1.name'和'bar'的't2.name',在't1_has_t2'中插入'data'爲123的記錄和另一個的主鍵兩個表,但只更新'數據',如果它已經存在這兩個主鍵。 – user1032531