SQL:将值插入外键
我正在使用 phpmyadmin 创建一个网站,并且有一个关于将值插入外键的问题。
当事件表中的事件列值为 x 即时,我能够选择图像表 id。
$event = "Partay";
$sql = "SELECT i.ImageID
FROM images i
INNER JOIN events e
ON i.eventID = e.eventID
WHERE e.event ='".$event."'";
这适用于选择要显示的图像。但是,当我想插入图像时,内部连接似乎工作不一样。
$sql = "INSERT INTO images i (i.image, e.event)
INNER JOIN event e
ON i.eventID = e.eventID
VALUES ('".$content."','".$event."')";
-我尝试将图像插入图像表中,并将新事件插入事件表中,以便该新图像属于该新事件。 - 另外,在插入具有现有事件的图像时,插入将如何工作?
任何帮助都会很好,谢谢。
Im using phpmyadmin for a website i'm creating and have a question about a inserting values into foreign key.
Im able to select an image table id when event col in events table is value x i.e...
$event = "Partay";
$sql = "SELECT i.ImageID
FROM images i
INNER JOIN events e
ON i.eventID = e.eventID
WHERE e.event ='".$event."'";
This works for selecting images to display. However when i want to insert and image inner join doesnt seem to work the same.
$sql = "INSERT INTO images i (i.image, e.event)
INNER JOIN event e
ON i.eventID = e.eventID
VALUES ('".$content."','".$event."')";
-Im trying to insert image into images table and a new event into event table so that this new image belongs to this new event.
-Also how would inserting work when inserting image with an existing event?
Any help would be good thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该首先插入事件,如果可以,然后插入图像(如果有)。
不知道你想如何做到这一点,但我认为你应该检查是否有你想要的插入或更新。
在这两种情况下,您都应该使用事务来确保插入您想要的所有内容或根本不插入。 (本例中为事件和图像)。
阅读有关事务和隔离级别的内容以更好地理解这一点。
You should INSERT first the event, if Ok, then INSERT the image if it has any.
Don't know, how you want to do that, but I think you should check if there is an insert or update that you want.
In both situation you should use a transaction to ensure that you insert all that you want or not at all. (Event and Image in this case).
Read something about transaction and isolation level to understand this better.
使用包含在事务中的两个单独的插入语句。
要为已存在的事件插入图像,只需使用图像的插入语句即可。
Use two separate insert statements wrapped in a transaction.
To insert an image for an event which already exists, just use the insert statement for the image.
SQL 语法无效。没有办法用 SQL 的一条指令来完成它。
插入事件,然后插入图像。如果你想确保原子性(确保两者都成功或不插入寄存器),请使用像 swarFish 所说的事务。
Invalid SQL Syntax. There in no way to do it in one instruction in SQL.
Insert in event, then insert in images. Use a transaction like swarFish said if you want to ensure atomicity (ensure that both are successful or no register is inserted).