sql - How to get the RowID to put into an INSERT INTO statement -


i have 2 tables
virtual_domains
virtual_users.

virtual_domains has list of valid domains emails server, example data:

     id         name     1          example.org     2          example.com     3          example.net     
virtual_users keeps valid email addresses each domain, , links them domain belong, example data:
     id         domain_id          password           email address     1          1                               jack@example.org     2          1                 thing              john@example.org     3          3                 goes               annete@example.net     4          2                 here               bilbo@example.com     
insert new user, use syntax (auto_increment in use id column):
insert databasename.virtual_users (     id,      domain_id,      password,      email )      values (     default,      '3',      md5('somepassword'),      'kurt@example.net');     
, add kurt@example.net 5th row email user.

what want able do, instead of putting in '3' domain id, add other syntax there return id virtual_domains table of relevent domain, ie:

insert `databasename`.`virtual_users` (  `id`,  `domain_id`,  `password`,  `email` )  values ( default,  *add_query_here_to_get_domian_id_value_from_virtual_domains_table*,  md5('somepassword'),  'kurt@example.net' ); 

so can done in 1 command, rather having domain_id in seperate query.

you can use subquery in insert statement.

 insert databasename.virtual_users (      id,       domain_id,       password,       email  )       values (      default,       ( select id databasename.virtual_domains name = 'example.net' ),       md5('somepassword'),       'kurt@example.net'  ); 

i have tested in sql server, think should work in databases.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -