简体   繁体   English

谁能帮我在不同的表中插入相同的ID

[英]Can anyone help me how to insert the same id in different tables

How to insert the same id in different tables at a time in one table. 如何在一个表中一次在不同表中插入相同的ID。 it is a primary key and autoincrement and in another table. 它是一个主键,并在另一个表中自动递增。 It is a foreign key at a time. 一次是外键。 I have to insert in both the tables using OpenCrat. 我必须使用OpenCrat在两个表中插入。

$this->db->query("INSERT INTO " 
. DB_PREFIX .   "xyz
SET 
    boutiques_id = '" . (int)$this->customer->getId() . "',
    boutique_customer_id = '" . $this->db->escape($data['boutique_customer_id']) . "',
    ordered_date = '" . $this->db->escape($data['ordered_date']) . "',
  '");

$this->db->query("INSERT INTO " 
. DB_PREFIX .   "abc 
SET 
    boutiques_id = '" . (int)$this->customer->getId() . "',
    firstname = '" . $this->db->escape($data['firstname']) . "', 
    lastname        = '" .$this->db->escape($data['lastname']). "',

    }

in abc table it is a primary key and autoincrement whereas in second table it is foreign key. 在abc表中,它是主键和自动递增,而在第二张表中,它是外键。

After an insert query use this $this->db->getLastId(); 插入查询后,使用此$this->db->getLastId(); to get last inserted id of that table, by this u can add this to another table. 要获取该表的最后插入的ID,可以将其添加到另一个表中。

The ghetto way assuming there is a unique constraint on boutique_customer_id and ordered_date would be: 假设在Boutique_customer_id和ordered_date上存在唯一约束的贫民区方式为:

INSERT INTO abc (boutiques_id, firstname, lastname)
SELECT boutiques_id, 'John', 'Smith'
FROM xyz
WHERE boutique_customer_id = 123
AND ordered_date = 2015-05-01

Or else you would need to use a transaction/programmatically get and set that foreign key value. 否则,您将需要使用事务/以编程方式获取并设置该外键值。 Either way I wouldn't try to set the primary key when initially inserting. 无论哪种方式,我在初次插入时都不会尝试设置主键。 Finally unrelated to your question, try looking at using an ORM. 最后,与您的问题无关,请尝试使用ORM。

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

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