插入多行

时间:2016-02-11 12:18:44

标签: php sql

我试图用另一个表中的值填充表的多行,但我有点卡住了。这是我到目前为止所做的。

SELECT radocs_id FROM radocs ORDER BY radocs_id DESC LIMIT 1,
SELECT crew_id FROM crew,
INSERT INTO signed (radocs_id, crew_id) Values ('radocs_id', 'crew_id');

SELECT radocs_id FROM radocs ORDER BY radocs_id DESC LIMIT 1,(按预期返回单个条目)

SELECT crew_id FROM crew,(按预期返回多个条目)

我想在表中为每个'crew_id'创建一个名为'signed'的条目,其中包含唯一的'radocs_id',但INSERT INTO signed (radocs_id, crew_id) Values ('radocs_id', 'crew_id')不起作用。

我做错了什么,或者这是错误的做法?

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

试试这个:

INSERT INTO signed 
(SELECT crew_id,(select radocs_id FROM radocs
                 ORDER BY radocs_id DESC LIMIT 1)
FROM crew)

答案 1 :(得分:0)

这是获取所需行的查询:

SELECT r.radocs_id, c.crew_id
FROM (SELECT radocs_id
      FROM radocs 
      ORDER BY radocs_id DES
      LIMIT 1
     ) r CROSS JOIN
     crews c;

然后插入是:

INSERT INTO signed(radocs_id, crew_id)
    SELECT r.radocs_id, c.crew_id
    FROM (SELECT radocs_id
          FROM radocs 
          ORDER BY radocs_id DES
          LIMIT 1
         ) r CROSS JOIN
         crews c;