我以前已经问过这个问题,但是随着select获得插入的所有变量,我该如何用零件字符串,零件选择查询执行插入?
例如:
INSERT INTO users (first_name, surname, foreign_id)
VALUES ('John', 'Smith', SELECT id FROM foreign_ids WHERE name = 'John')
答案 0 :(得分:3)
以这种方式使用插入选择
INSERT INTO users (first_name, surname, foreign_id)
SELECT 'John', 'Smith', id FROM foreign_ids
WHERE name = 'John';
将值中的文字字符串作为文字字符串移动到select的相应列中
答案 1 :(得分:0)
为您的选择查询
创建变量 con
然后将Cannot assign to 'con' because it is a 'using variable'
放入您的插入查询
$sql = "SELECT id FROM foreign_ids WHERE name = 'John'";
$foreign_id = $conn->query($sql);