简体   繁体   English

MySQL插入到多个表(关系)

[英]MySQL insert to multiple tables (relational)

tbl_product
Name | Creator | UID | Salerank

tbl_price
Supplier | Price | UID

I want to insert a product and then insert multiple prices into a seperate table. 我想插入一个产品,然后在一个单独的表中插入多个价格。 How is it possible to ensure that both tables had the same UID ideally an auto increment field? 如何保证两个表都具有相同的UID(理想情况下是自动递增字段)? I will be using PHP alongside MySQL. 我将与MySQL一起使用PHP。

Thanks, 谢谢,

J Ĵ

Make UID an auto_increment primary key on the products table, but just a regular primary key on the prices table (no auto_increment). 在产品表上将UID设置为auto_increment主键,而在价格表上将其设置为常规主键(不能使用auto_increment)。 After you insert itnto products, use the PHP command mysql_insert_id() . 插入itnto产品后,使用PHP命令mysql_insert_id() This will get the ID generated from the last query, which will be your UID generated on the products table. 这将获取从上一个查询生成的ID,这将是您在products表上生成的UID。 Assign it a variable and use it in your insert statement on the prices table. 给它分配一个变量,并在价格表的插入语句中使用它。

http://php.net/manual/en/function.mysql-insert-id.php http://php.net/manual/en/function.mysql-insert-id.php

使用GUID作为UID,或者更好的方法是插入您的产品,并使用例如产品名称(假设唯一)插入价格以查找相关产品UID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM