简体   繁体   中英

mysql query one-to-many relationship

+---------+    +-----------+   +---------+
| USER    |    | USER_LANG |   | LANG    |
| id_user |    | id_user   |   | id_lang |
| name    |    | id_lang   |   | name    |
|         |    | years     |   |         |
+---------+    +-----------+   +---------+

I want to write query for saving data from user and user_lang in database at same time...is there some insert join or what?

No there isn't. You can only select or delete from multiple tables at once.

If table structure would have been same; it would have been possible. But in your case you will have to use multiple queries. If you want to ensure integrity between table data then use Stored Procedures.

try this

   INSERT INTO LANG (id_lang,name) 
   VALUES    (SELECT ul.id_lang ,u.name 
              FROM `USER` u
              INNER JOIN `USER_LANG` ul 
              ON  u.id_user = ul.id_user 
              )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM