[英]MySQL insert multiple row select in other table
I have small database with couple of tables for some small PHP project. 我有一个带有几个表的小型数据库,用于一些小型PHP项目。 One of my SELECTs (given bellow table) gives result like this: 我的SELECT之一(给定的风箱表)给出了如下结果:
+-------+------+------+ + ------- + ------ + ------ +
| | idpart | idpart | qty |IMEI | 数量| IMEI |
+-------+------+------+ + ------- + ------ + ------ +
| | 2 | 2 | 4 | 4 | xxx | xxx |
| | 6 | 6 | 1 | 1 | yyyy | yyyy |
| | 8 | 8 | 2 | 2 | zzzz | zzzz |
|10 | | 10 | 3 | 3 | ssss | ssss |
+-------+------+------+ + ------- + ------ + ------ +
Number of rows changes it can be 1 or n, but its never less then 1. idpart is not repeating it self as result of this query - it can be show only once. 行更改的数量可以为1或n,但永远不会小于1。idpart不会由于此查询而自我重复-只能显示一次。 This is the actual query: 这是实际的查询:
select odel_part.idpart as idpart, model_part.qtyused as qty, reparationorders.IMEI as IMEI
from reparation orders
inner join order model on order_model.idreparationorder=reparationorders.idreparationorder
inner join models on order_model.idmodel = models.idmodel
inner join model_part on models.idmodel = model_part.idmodel
inner join parts on model_part.idpart = parts.idpart
where reparationorders.idreparationorder = 1
Result of this query along with some additional data which is fixed has to be inserted in to other table . 该查询的结果以及一些固定的其他数据必须插入到其他表中 。 Other table has following fields: 其他表具有以下字段:
+-----------+-----------+-------+--------+-----+-------+ + ----------- + ----------- + ------- + -------- + ----- +- ----- +
| | idtrans | idtrans | idpart | idpart | qty | 数量 date | 日期| tt | tt | IMEI | IMEI |
+-----------+-----------+-------+--------+-----+-------+ + ----------- + ----------- + ------- + -------- + ----- +- ----- +
idtrans - int which autoincrements idtrans-自动增值的 int
idpart - from query ( idpart ) idpart-来自查询( idpart )
qty - from query ( qty ) 数量 -来自查询( 数量 )
date - entered manualy 日期 -手动输入
tt - entered manualy tt-手动输入
IMEI - from query ( IMEI ) IMEI-来自查询( IMEI )
in this 2nd table idtrans is unique, idpart can repeat it self thorough rows (this is intended behaviour, because this table will track usage of this parts for different dates). 在此第二个表中,idtrans是唯一的,idpart可以自我重复行(这是预期的行为,因为此表将跟踪该部分在不同日期的用法)。 Can you help me with doing this insert to 2nd table (name of 2nd table is transactions)? 您能帮我插入第二张表吗(第二张表的名称是事务)?
Thank you in advance 先感谢您
You would just do: 您只需执行以下操作:
insert into transactions(idpart, qty, date, tt, IMEI)
select idpart, qty, now(), @tt, imei
from reparations orders . . .;
The . . .
的. . .
. . .
is just the rest of your query. 只是您查询的其余部分。 I am guessing that you want the current date/time inserted for "date"; 我想您要为“日期”插入当前日期/时间; now()
provides that information. now()
提供了该信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.