我以前见过这样的问题,但是select获取了insert的所有变量,如何使用part string和part select查询执行insert?例如:
INSERT INTO users (first_name, surname, foreign_id) VALUES ('John', 'Smith', SELECT id FROM foreign_ids WHERE name = 'John')
hrirmatl1#
为select查询创建变量 $sql = "SELECT id FROM foreign_ids WHERE name = 'John'"; $foreign_id = $conn->query($sql); 然后把 $foreign_id 插入查询内部 INSERT INTO users (first_name, surname, foreign_id) VALUES ('John', 'Smith', '{$foreign_id}')
$sql = "SELECT id FROM foreign_ids WHERE name = 'John'"; $foreign_id = $conn->query($sql);
$foreign_id
INSERT INTO users (first_name, surname, foreign_id) VALUES ('John', 'Smith', '{$foreign_id}')
x3naxklr2#
使用插入选择方式
INSERT INTO users (first_name, surname, foreign_id) SELECT 'John', 'Smith', id FROM foreign_ids WHERE name = 'John';
将值中的文字字符串作为文字字符串移动到select中相应列中
2条答案
按热度按时间hrirmatl1#
为select查询创建变量
$sql = "SELECT id FROM foreign_ids WHERE name = 'John'"; $foreign_id = $conn->query($sql);
然后把$foreign_id
插入查询内部INSERT INTO users (first_name, surname, foreign_id) VALUES ('John', 'Smith', '{$foreign_id}')
x3naxklr2#
使用插入选择方式
将值中的文字字符串作为文字字符串移动到select中相应列中