我有2个表,一篇文章和一个摘要表。
摘要表是文章表的子项,我想通过文章表将数据添加到摘要表中。
摘要表的sql如下:
CREATE TABLE summaries(
summary_id INT NOT NULL AUTO_INCREMENT,
article_id INT,
summary TEXT,
PRIMARY KEY(summary_id),
FOREIGN KEY(article_id) REFERENCES articles(article_id)
)ENGINE=INNODB;
如何将摘要添加到摘要表中,并使article_id等于我的文章表格中的article_id?
以下是我将数据保存到文章表的方法:
$sql = "INSERT INTO articles (url, domain, favicon, title) VALUES ( '$url','$domain','$favicon','$title')";
if (mysql_query($sql)){
$s = "SELECT max(article_id) from articles";
$object1 = mysql_query($s);
}
这是我正在尝试做的事情:
$large_summary = $article_array['summary'];
foreach ($large_summary as $summary){
// $summary_sql = "INSERT INTO summaries SET `summary`=(SELECT `sum`.`summary` FROM summaries as sum, articles as art WHERE art.article_id=sum.article_id AND art.article_id=$object1 -> article_id)";
$summary_sql = "INSERT INTO `summaries` (summary,article_id) VALUES ('$summary' , ('SELECT article_id FROM articles WHERE article.article_id=summary.article_id AND article.article_id = $object1 -> article_id' ))";
echo $summary_sql;
$summary_sql_query = mysql_query($summary_sql);
if(!mysql_query($sql2)){
die('Error: ' . mysql_error());
}
echo "$summary <br>";
}
我正在遍历large_summary数组以获取一个单独的摘要,我想通过它的文章保存到数据库
答案 0 :(得分:0)
INSERT INTO summaries SET `summary`=
(SELECT `art`.`summary` FROM summaries as sum, articles as art WHERE
art.article_id=sum.article_id AND art.article_id=123)
Here '123' is a sample article_id i am passing. You have to pass your real article_id
答案 1 :(得分:0)
试试:
INSERT INTO `summaries` (summary,article_id)
VALUES ($yoursummryVar , (SELECT article_id
FROM articles
WHERE ........)
)