[英]Insert mysql on foreign key id php
I have 2 tables, clubs and fixtures 我有2张桌子,俱乐部和固定装置
clubs 会所
id (int)
name (text)
fixtures 灯具
id (int)
hometeam (int, foreign key to clubs id)
awayteam (int, foreign key to clubs id)
datetime (datetime)
Each fixtures record uses an ID for the hometeam
and awayteam
as per the foreign key relationship. 每个固定记录都根据外键关系
hometeam
和awayteam
使用ID。
I need to do an insert into the fixtures
table, but I only have the hometeam
name not the hometeam
id. 我需要在
fixtures
表中进行插入,但是我只有hometeam
名称,而没有hometeam
id。 Is there a way of doing this through the foreign key relationship, without having to separately lookup the relevant id number? 是否可以通过外键关系来执行此操作,而不必分别查找相关的ID号?
There is nothing wrong with looking for foreign key value through a separate select query: 通过单独的选择查询来查找外键值没有任何问题:
INSERT INTO `fixtures`
VALUES ( NULL,
(SELECT `id` FROM `clubs` WHERE `name` = 'NAME'),
AWAYTEAM_ID,
CURRENT_TIMESTAMP
);
You need to do in 2 steps: 您需要执行以下两个步骤:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.