Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用Select查询插入_Mysql_Insert - Fatal编程技术网

Mysql 使用Select查询插入

Mysql 使用Select查询插入,mysql,insert,Mysql,Insert,我以前见过这样的问题,但是select获取了插入的所有变量,我如何使用部分字符串执行插入,部分select查询 例如: INSERT INTO users (first_name, surname, foreign_id) VALUES ('John', 'Smith', SELECT id FROM foreign_ids WHERE name = 'John') 使用“插入”选择此方式 INSERT INTO users (first_name, surname, foreign_id)

我以前见过这样的问题,但是select获取了插入的所有变量,我如何使用部分字符串执行插入,部分select查询

例如:

INSERT INTO users (first_name, surname, foreign_id) 
VALUES ('John', 'Smith', SELECT id FROM foreign_ids WHERE name = 'John')

使用“插入”选择此方式

INSERT INTO users (first_name, surname, foreign_id)  
SELECT 'John', 'Smith',  id FROM foreign_ids 
WHERE name = 'John';

将值中的文字字符串作为文字字符串移动到select

中的相应列中,为您的select查询创建变量

$sql=“从外部id中选择id,其中name='John';
$foreign\u id=$conn->query($sql)

然后将
$foreign\u id
放入插入查询


插入用户(名字、姓氏、外国id)值('John'、'Smith'、'{$foreign\u id}')

感谢您的帮助。