MYSQL IN语句结果使INSERT成为可能

时间:2018-01-09 00:01:09

标签: sql

从CSV文件填充两个表:

路由器:

+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int(11)      | NO   | PRI | NULL    | auto_increment |
| type_id          | int(11)      | YES  | MUL | NULL    |                |
| identificationID | varchar(255) | NO   |     | NULL    |                |
| wep              | varchar(255) | NO   |     | NULL    |                |
| ssid             | varchar(255) | NO   | UNI | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+

拾音器:

+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| id                 | int(11)      | NO   | PRI | NULL    | auto_increment |
| type_id            | int(11)      | YES  | MUL | NULL    |                |
| enable             | tinyint(1)   | YES  |     | NULL    |                |
| name               | varchar(255) | NO   |     | NULL    |                |
| adress             | varchar(255) | YES  |     | NULL    |                |
| zip                | varchar(255) | YES  |     | NULL    |                |
| city               | varchar(255) | YES  |     | NULL    |                |
| googleGeoCode      | varchar(255) | YES  |     | NULL    |                |
| agent              | tinyint(1)   | YES  |     | NULL    |                |
| userRestricted     | tinyint(1)   | YES  |     | NULL    |                |
| superUserPickup_id | int(11)      | YES  | UNI | NULL    |                |
| telephone          | varchar(255) | YES  |     | NULL    |                |
+--------------------+--------------+------+-----+---------+----------------+

因为一切都来自Excel,我需要一个SQL代码来填充第三个表

pickup_has_router:

+-----------+----------+------+-----+-------------------+----------------+
| Field     | Type     | Null | Key | Default           | Extra          |
+-----------+----------+------+-----+-------------------+----------------+
| id        | int(11)  | NO   | PRI | NULL              | auto_increment |
| router_id | int(11)  | YES  | MUL | NULL              |                |
| pickup_id | int(11)  | YES  | MUL | NULL              |                |
| timeStamp | datetime | NO   |     | CURRENT_TIMESTAMP |                |
+-----------+----------+------+-----+-------------------+----------------+

例如,我知道下面的路由器的拾取ID为1,我知道如何获取它们

SELECT * FROM `routers` WHERE `identificationID` IN(
'UCDDU17805001464',
'UCDDU17805001029',
'UCDDU17805000137',
'UCDDU17805000129',
'UCDDU17805001394'
)

但是根据上述结果,如何循环INSERT语句,如下所示:

INSERT INTO `pickup_has_router` (`id`, `router_id`, `pickup_id`, `timeStamp`) 
WHERE `identificationID` IN(
    'UCDDU17805001464',
    'UCDDU17805001029',
    'UCDDU17805000137',
    'UCDDU17805000129',
    'UCDDU17805001394'
    )
VALUES (NULL, 'router.id', '1', CURRENT_TIMESTAMP);

感谢您的帮助..我一直在搜索很多,是的SQL不是我最好的技能: - ))))))

1 个答案:

答案 0 :(得分:1)

  insert into `pickup_has_router` (`router_id`, `pickup_id`) 
  select `id`, 1 from `routers`
    where `identificationID` in(
        'UCDDU17805001464',
        'UCDDU17805001029',
        'UCDDU17805000137',
        'UCDDU17805000129',
        'UCDDU17805001394'
        )

执行插入时,可以使用select语句而不是values子句,其中select语句返回的列与插入所需的列匹配。