在外键ID php上插入mysql [英] Insert mysql on foreign key id php

查看:63
本文介绍了在外键ID php上插入mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子,球杆和固定装置

I have 2 tables, clubs and fixtures

俱乐部

id (int)
name (text)

灯具

id (int)
hometeam (int, foreign key to clubs id)
awayteam (int, foreign key to clubs id)
datetime (datetime)

每个固定装置记录根据外键关系使用hometeamawayteam的ID.

Each fixtures record uses an ID for the hometeam and awayteam as per the foreign key relationship.

我需要在fixtures表中进行插入,但是我只有hometeam名称而不是hometeam id.有没有一种方法可以通过外键关系来执行此操作,而不必分别查找相关的ID号?

I need to do an insert into the fixtures table, but I only have the hometeam name not the hometeam id. Is there a way of doing this through the foreign key relationship, without having to separately lookup the relevant id number?

推荐答案

通过单独的select查询查找外键值没有错:

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
    );

这篇关于在外键ID php上插入mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆