如何将值插入到 mySql 上具有外键关系的两个表中
How to insert values into two tables with a foreign key relationship on mySql
npas
+-----+------+---------------------------+
| id | npa | town |
+-----+------+---------------------------+
| 1 | 104 | Villars-le-terroir |
+-----+------+---------------------------+
客户
+----+----------+-----------+------------------------+---------------+---------------------------+---------------+--------+
| id | lastname | firstname | address | phone | email | mobile | npa_id |
+----+----------+-----------+------------------------+---------------+---------------------------+---------------+--------+
| 1 | Gentizon | Alain | Derrière le Chateaux 6 | 021 000 00 00 | xxxxx.xxxxxx@xxxxx.com | 077 000 00 00 | 96 |
+----+----------+-----------+------------------------+---------------+---------------------------+---------------+--------+
INSERT INTO customers (lastname,firstname,address,email,mobile)
VALUES ('xycyxc', 'xcyc', 'xycyxc', 'xycyxcy', 'xycyxc');
INSERT INTO npas (npa,town)
VALUES ('xycyxcy', 'xycyxc')
我该怎么办?
如果我的问题与其他问题重复,我很抱歉。我比 SQL 更新鲜。感谢您的帮助
INSERT INTO npas (npa, town)
VALUES ('npa', 'town');
INSERT INTO customers (lastname, firstname, address, email, mobile, npa_id)
SELECT 'lastname', 'firstname', 'address', 'email', 'mobile', npas.id
FROM npas
WHERE npas.npa = 'npa' AND npas.town = 'town';
npas
+-----+------+---------------------------+
| id | npa | town |
+-----+------+---------------------------+
| 1 | 104 | Villars-le-terroir |
+-----+------+---------------------------+
客户
+----+----------+-----------+------------------------+---------------+---------------------------+---------------+--------+
| id | lastname | firstname | address | phone | email | mobile | npa_id |
+----+----------+-----------+------------------------+---------------+---------------------------+---------------+--------+
| 1 | Gentizon | Alain | Derrière le Chateaux 6 | 021 000 00 00 | xxxxx.xxxxxx@xxxxx.com | 077 000 00 00 | 96 |
+----+----------+-----------+------------------------+---------------+---------------------------+---------------+--------+
INSERT INTO customers (lastname,firstname,address,email,mobile)
VALUES ('xycyxc', 'xcyc', 'xycyxc', 'xycyxcy', 'xycyxc');
INSERT INTO npas (npa,town)
VALUES ('xycyxcy', 'xycyxc')
我该怎么办?
如果我的问题与其他问题重复,我很抱歉。我比 SQL 更新鲜。感谢您的帮助
INSERT INTO npas (npa, town)
VALUES ('npa', 'town');
INSERT INTO customers (lastname, firstname, address, email, mobile, npa_id)
SELECT 'lastname', 'firstname', 'address', 'email', 'mobile', npas.id
FROM npas
WHERE npas.npa = 'npa' AND npas.town = 'town';