如何在mysql的子表中插入数据

如何在mysql的子表中插入数据

本文介绍了如何在mysql的子表中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表,一个文章表和一个摘要表.

I have 2 tables, an articles and a summaries table.

summarys表是articles表的子级,我想通过articles表将数据添加到summarys表中.

The summaries table is a child of the articles table and I would like to add data into the summaries table through the articles table.

汇总表的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等于我的articles表中的article_id?

How do I add a summary into the summaries table and have the article_id equal to the article_id in my articles table?

这是我将数据保存到商品表的方式:

Here is how I'm saving my data to articles table:

 $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数组以获取单个摘要,该摘要要通过其适用的文章保存到数据库中

I'm looping through the large_summary array to obtain an individual summary which I want to save to the database via the article it is for

推荐答案

尝试:

INSERT INTO `summaries` (summary,article_id)
    VALUES ($yoursummryVar , (SELECT article_id
                              FROM articles
                              WHERE ........)
    )

这篇关于如何在mysql的子表中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:43