【问题标题】:Inserting one column each from two different tables to 2 column of a table将两个不同表中的一列分别插入到表的 2 列中
【发布时间】:2015-07-12 18:20:00
【问题描述】:

这是可能的示例数据库

TABLE A-----> TABLE B <-----TABLE C

箭头表示关系

TABLE A 
{ 
   tblA_col1 <----primary key
} 
TABLE A
 (tblA_col1)
  | A_1  |<----data


TABLE C 
{ 
  tblC_col1 <----primary key
}
TABLE C
 (tblC_col1)
  | C_1  |<----data
  | C_2  |
  | C_3  |


TABLE B
{
  tblB_col1 <----primary key
  tblA_col1 <----col from table A
  tblC_col1 <----col from table C
}
TABLE B
(tblB_col1 |  tblA_col1  |  tblC_col1)
|   B_1    |     A_1     |    C_1    |
|   B_2    |     A_1     |    C_2    |
|   B_3    |     A_1     |    C_3    |   

现在我的问题是如何只用一个查询在TABLE B 中插入数据?

【问题讨论】:

    标签: sql codeigniter


    【解决方案1】:
    insert into B (tblA_col1, tblC_col1 )
    select A.tblA_col1, C.tblC_col1 from 
    A, C; 
    

    这是你想要的吗?

    【讨论】:

    • 是的,谢谢,我之前试过但没用..然后我再试一次它有效!
    • 哇,我以为这不可能。虽然从未尝试过。但我认为它很好并且有效。
    • 是的,它可以工作,现在我的问题是如何将其转换为在 Codeigniter 中插入数据
    • 你知道这个查询。所以你可以使用$this-&gt;db-&gt;query($query);
    • 得到一个错误 public function insert_req($s_no, $filed, $dat) { $sql = "Insert into student_requirements (filed, datesubmitted, documents, r_no, s_no)" 。 "($filed, $dat, (Select a.description, a.r_no, b.s_no from requirements a, student b where s_no = $s_no))";返回 $sql->result(); } 可以做些什么呢?
    【解决方案2】:

    请参考这个帖子,因为答案与您的问题有关。

    MySQL 不支持在单个 INSERT 语句中插入多表

    但是,您可以使用一个事务并将它们都包含在一个事务中

    sql - insert into multiple tables in one query

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多