php, pdo mysql 不能用内连接插入 [英] php, pdo mysql can't insert with inner join
本文介绍了php, pdo mysql 不能用内连接插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我对以下内容有些困惑.我有两个表,项目和变更.
So I'm a little stuck with the following. I have two tables, projects and change.
项目:
- id
- title
- description
- datecreated
变化:
- id
- title
- description
- projectid FOREIGN KEY
- datecreated
我不知道怎么做
insert into change (name, description, projectid) value (:name, :description, :projectid)
select id from project
where name = $name
重要 - 插入中的名称和描述由 php 变量使用表单提供.
important - the name and description in the insert are provided by php variables using a form.
重要 - 必须使用 PDO
important - must use PDO
实际代码:
$sql = "INSERT INTO change (title, description, project_id) SELECT :title, :description, id FROM project WHERE title = :project_title";
$query = $db->prepare($sql);
$query->execute(array(":title" => $title, ":description" => $description, ":project_title" => $created));
这就是我最后所做的,但 Barmar 在这个问题和另一个问题上得到了帮助.
This is what I did in the end but Barmar gets the point for help on this and another question.
$sql = "INSERT INTO `change` (`title`, `description`, `project_id`) SELECT :title, :description, id FROM project WHERE title = :project_title";
$query = $db->prepare($sql);
$query->execute(array(":title" => $title, ":description" => $description, ":project_title" => $created));
推荐答案
插入时,要么使用 values
子句,要么使用 select
指定源数据.不能同时使用.
When you insert, you either use a values
clause or you use select
to specify the source of the data. You can't use both.
你想要:
INSERT INTO `change` (`title`, `description`, `projectid`)
SELECT :title, :description, id
FROM project
WHERE title = :project_title
这篇关于php, pdo mysql 不能用内连接插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文