drbfxb977777 2019-06-10 12:49
浏览 106
已采纳

MySQL插入具有不同列名的表

I have 3 tables. "machines", "products", "machine_products".

The idea in my mind is to store machine information in "machines" table and set "machine_id" column as PRIMARY & UNIQUE and do same with "products" table the column name here is "product_id".

One machine may have one or more product so they both match in "machine_products" table

Table samples are below.

machines table

+------------+------------------+
| machine_id |   machine_code   |
+------------+------------------+
|      1     | C01.C03.C23 M.1  |
+------------+------------------+
|      2     | C07.08.09.10 M.1 |
+------------+------------------+
|      3     | C11.12 MONT.1    |
+------------+------------------+
|      4     | C13.14.21 MONT.1 |
+------------+------------------+
|      5     | C22 MONT.1       |
+------------+------------------+

products table

+------------+--------------+
| product_id | product_code |
+------------+--------------+
|      1     | C01.00       |
+------------+--------------+
|      2     | C01.11       |
+------------+--------------+
|      3     | C01.21       |
+------------+--------------+
|      4     | C03.00       |
+------------+--------------+
|      5     | C03.01       |
+------------+--------------+

machine_products table

+----+------------+------------+
| id | machine_id | product_id |
+----+------------+------------+
|  1 |      1     |     70     |
+----+------------+------------+
|  2 |      1     |     73     |
+----+------------+------------+
|  3 |      1     |     78     |
+----+------------+------------+
|  4 |      1     |     83     |
+----+------------+------------+
|  5 |      2     |     100    |
+----+------------+------------+
|  6 |      2     |     208    |
+----+------------+------------+
|  7 |      3     |     101    |
+----+------------+------------+
|  8 |      3     |     108    |
+----+------------+------------+
|  9 |      3     |     112    |
+----+------------+------------+
| 10 |      4     |     113    |
+----+------------+------------+

My problem is I want to insert data to "machine_products" table by using "machine_code" column data

How do I add records to the machine_products table when what I have are the product_code and machine_code?

  • 写回答

3条回答 默认 最新

  • douxu0550 2019-06-10 17:58
    关注

    Can you try this

    Insert into machine_products ( machine_id, product_id) values( (select machine_id from machines where machine_code = $machine_code), (select product_id from products where product_code=$product_code))
    

    You can the substitute $machine_code and $product_code for your own values

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路