简单地为插入的mysql查询

时间:2017-02-27 19:07:33

标签: mysql sql-insert

我正在创建一个新的查询,用于将值插入到一个表中,该表从另一个表中提取值

 INSERT INTO user (EmailAddress, InternalPhone, ExternalPhone,  CreatedBy, DateCreated,roll_key,user_key )
 VALUES ( NULL, NULL, NULL, 1, 'dba', Now(),  
 (select r.roll_key from user u
    join work wrk on u.work_key = wrk.Work_key
    join roll r on r.Name = 'Ext'
    where u.Name = 'test'),
 (select u.user_key from user u
    join work wrk on u.work_key = wrk.Work_key
    join roll r on r.Name = 'Ext'
    where u.Name = 'test'))

如何简单地使用此查询而不是将select语句放入值中。

1 个答案:

答案 0 :(得分:2)

您可以使用单个选择并将固定值分配给伪列

  INSERT INTO user (EmailAddress, InternalPhone, ExternalPhone,  CreatedBy, DateCreated,roll_key,user_key )
  select   NULL, NULL, 1, 'dba', Now(),    r.roll_key, u.user_key
  from user u
  join work wrk on u.work_key = wrk.Work_key
  join roll r on r.Name = 'Ext'
  where u.Name = 'test'