如何基于tableB记录行在tableA中插入多个记录?

时间:2018-08-07 02:48:53

标签: mysql insert sql-insert

我的表A和表B如下:-

表A

html {
  margin: auto;
  padding-top: 255px;
  padding-bottom: 255px;
  padding-right: 55px;
  padding-left: 55px;
}

h3 {
  text-transform: uppercase;
}

head,
h1,
h3 {
  position: static;
  text-align: center;
  font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}

body {
  background-color: whitesmoke;
}

#itemkeyname {
  position: static;
  text-align: center;
  font-family: Arial, Helvetica, sans-serif;
  text-transform: uppercase;
}

#contentGenerate,
#generatebutton {
  position: static;
  text-align: center;
  font-family: monospace;
}

#generatebutton {
  border-top: 0.5px;
  border-bottom: 0.5px;
  border-style: none;
}

表B

<!DOCTYPE html>
<html>

<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Page Title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" media="screen" href="snippet.css" />

</head>

<body>

  <div id="wrapped">

    <div id="insideItems">
      <div id="itemkeyname"></div>
    </div>

  </div>

  <div id="itemArrayGenerate">
    <button type="button" id="generatebutton" value="generate">Generate</button>
  </div>

  <script src="snippet.js"></script>

</body>

</html>

我可以知道如果Category_id为1且user_id为5且is_removed为false的情况下如何在表B的表A上插入多条记录? 谢谢。

1 个答案:

答案 0 :(得分:1)

简短答案:

给出了两个表格:

insert into A (select 5, sub_category_id, category_id, false from B where category_id = 1);

详细说明:

内容:

select * from A;
+---------+-----------------+-------------+------------+
| user_id | sub_category_id | category_id | is_removed |
+---------+-----------------+-------------+------------+
|       5 |             201 |           2 |          0 |
+---------+-----------------+-------------+------------+

B内容:

select * from B;
+-----------------+-------------+
| sub_category_id | category_id |
+-----------------+-------------+
|             101 |           1 |
|             102 |           1 |
|             103 |           1 |
|             201 |           2 |
+-----------------+-------------+

创建视图:

select 5, sub_category_id, category_id, false from B where category_id = 1;
+---+-----------------+-------------+-------+
| 5 | sub_category_id | category_id | FALSE |
+---+-----------------+-------------+-------+
| 5 |             101 |           1 |     0 |
| 5 |             102 |           1 |     0 |
| 5 |             103 |           1 |     0 |
+---+-----------------+-------------+-------+

插入:

insert into A (select 5, sub_category_id, category_id, false from B where category_id = 1);
+---------+-----------------+-------------+------------+
| user_id | sub_category_id | category_id | is_removed |
+---------+-----------------+-------------+------------+
|       5 |             201 |           2 |          0 |
|       5 |             101 |           1 |          0 |
|       5 |             102 |           1 |          0 |
|       5 |             103 |           1 |          0 |
+---------+-----------------+-------------+------------+