MySQL从2个源表插入到一个目标表

时间:2015-05-31 19:23:54

标签: mysql sql insert foreign-keys left-join

我在将两个表中的Id字段插入第三​​个表中的单个记录时遇到问题。

mysql> describe ing_titles;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| ID_Title | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| title    | varchar(64)      | NO   | UNI | NULL    |                |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.22 sec)

mysql> describe ing_categories;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| ID_Category | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| category    | varchar(64)      | NO   | UNI | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
2 rows in set (0.02 sec)

mysql> describe ing_title_categories;
+-------------------+------------------+------+-----+---------+----------------+
| Field             | Type             | Null | Key | Default | Extra          |
+-------------------+------------------+------+-----+---------+----------------+
| ID_Title_Category | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| ID_Title          | int(10) unsigned | NO   | MUL | NULL    |                |
| ID_Category       | int(10) unsigned | NO   | MUL | NULL    |                |
+-------------------+------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)   

让我们说表中的数据是:

mysql> select * from ing_titles;
+----------+-------------------+
| ID_Title | title             |
+----------+-------------------+
|        3 | Chicken           |
|        2 | corn              |
|        1 | Fettucini Alfredo |
+----------+-------------------+
3 rows in set (0.00 sec)

mysql> select * from ing_categories;
+-------------+----------+
| ID_Category | category |
+-------------+----------+
|           1 | Dinner   |
|           3 | Meat     |
|           2 | Veggie   |
+-------------+----------+
3 rows in set (0.00 sec)  

我想插入ing_title_categories记录"玉米,素食"或ID_Title = 2且ID_Category = 2。

这是我尝试的内容:

INSERT INTO ing_title_categories (ID_Title, ID_Category)
SELECT ing_titles.ID_Title, ing_categories.ID_Category
FROM ing_title_categories
    LEFT JOIN ing_titles ON ing_title_categories.ID_Title=ing_titles.ID_Title
    LEFT JOIN ing_categories ON ing_title_categories.ID_Category=ing_categories.ID_Category
WHERE (ing_titles.ID_Title=2) AND (ing_categories.ID_Category = 2);

ing_title_categories中没有插入数据,这是MySQL的回复:

Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

ing_titles.ID_Titleing_categories.ID_Category插入表ing_titles_categories的正确语法是什么?

请不要使用PHP或Python示例。使用我可以复制并粘贴到MySQL提示符的SQL。我将把它添加到C ++程序中,而不是PHP,JavaScript或Python。

编辑1: ing_title_categories.ID_Titleing_title_categories.ID_Category是其他表中的外键。

3 个答案:

答案 0 :(得分:0)

在这种情况下,我看到的唯一可能方式是使用UNION查询,例如

INSERT INTO ing_title_categories (ID_Title, ID_Category)

SELECT Title, NULL
FROM ing_title WHERE ID_Title = 2

UNION

SELECT NULL, category
FROM ing_categories
WHERE ID_Category = 2

(OR)

您可以更改表格设计并使用AFTER INSERT触发器一次执行相同操作。

修改

如果您可以将表格设计更改为类似下面的内容(不需要额外的链接表)

ing_titles(ID_Title int not null auto_increment PK, title varchar(64) not null);

ing_categories( ID_Category int not null auto_increment PK,  
category varchar(64) not null, 
ing_titles_ID_Title int not null, 
FOREIGN KEY (ing_titles_ID_Title) 
        REFERENCES ing_titles(ID_Title));

然后你可以使用AFTER INSERT触发器并像

一样进行插入
DELIMITER //

CREATE TRIGGER ing_titles_after_insert
AFTER INSERT
   ON ing_titles FOR EACH ROW

BEGIN


   -- Insert record into ing_categories table
   INSERT INTO ing_categories
   ( category,
     ing_titles_ID_Title)
   VALUES
   ('Meat' NEW.ID_Title);

END; //

DELIMITER ;

答案 1 :(得分:0)

INSERT INTO
    ing_title_categories (ID_Title, ID_Category)
SELECT
    ing_titles.ID_Title, ing_categories.ID_Category
FROM
    ing_titles, ing_categories
WHERE
    ing_titles.ID_Title = ing_categories.ID_Category AND
    ing_titles.ID_Title = 2 AND ing_categories.ID_Category = 2;

SQL Fiddle demo

答案 2 :(得分:0)

在接受@DrewPierce和@ KaiserM11的建议后,这是MySQL序列:

mysql> INSERT INTO ing_title_categories (ID_Title, ID_Category)
    -> SELECT
    ->     ing_titles.ID_Title,
    ->     ing_categories.ID_Category
    -> FROM ing_titles, ing_categories
    -> where (ing_titles.ID_Title = 2) AND (ing_categories.ID_Category = 2)
    -> ;
Query OK, 1 row affected (0.07 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from ing_title_categories;
+-------------------+----------+-------------+
| ID_Title_Category | ID_Title | ID_Category |
+-------------------+----------+-------------+
|                17 |        2 |           2 |
+-------------------+----------+-------------+
1 row in set (0.00 sec)