mysql - Insert into a table from two different tables -


here have 3 tables.

  • sbs_users
  • sbs_permissions
  • sbs_user_permissions

let's data of each table are:

- sbs_users(table1) userid    username 1         john 2         albert  - sbs_permissions(table2) permissionid    permission 1               create 2               edit  - sbs_user_permissions(table3) upid(autoid)    userid    permissionid 

what wanna insert table3 data table2 last id in table1.

so expected after insert is:

- sbs_user_permissions(table3) upid(autoid)    userid    permissionid 1               2         1 2               2         2 

thanks in advance

if read correctly, want give permissions user max user id? if so, this:

insert user_permissions select u.id, p.id   users u cross join permissions p   u.id = (select max(id) users); 

demo here


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -