简体   繁体   English

将值从一个表插入另一个表

[英]Inserting value from one table into another

Im trying to insert a value once its created from one table into another I have two databases 'mem' and 'location' 我试图在一个表中创建一个值后将其插入另一个表中,我有两个数据库“ mem”和“ location”

I want to add the primary key 'id' from the mem table and insert it into a column 'user_id' in the location table. 我想从内存表中添加主键“ id”,并将其插入位置表中的“ user_id”列中。

I have a sql query in my registration form page that auto increments the 'id' in the mem table yet does not seem to add the same value into the user_id in the location table, 我的注册表单页面中有一个sql查询,该查询会自动增加mem表中的“ id”,但似乎未在位置表的user_id中添加相同的值,

$id = mysql_insert_id(); mysql_query("INSERT INTO location (user_id) 
            VALUES (SELECT id FROM mem)");

Can someone please help! 有人可以帮忙吗?

The proper query is: 正确的查询是:

INSERT INTO location (user_id) SELECT id FROM mem

However, this query will actually insert all id values from 'mem' into 'location'. 但是,此查询实际上会将“ mem”中的所有 id值插入“ location”中。 To add the most recent id, you will want to use 要添加最新的ID,您将需要使用

INSERT INTO location (user_id) VALUES ($id)

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

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