简体   繁体   中英

How can i insert data in table with a join sql?

i have 2 tables on db. First table:

users

id|name|mail|password|group

second table

scores

id|name|score

The idea is get the name from users using the id (this id already know because i get that by php), then insert a score in table scores using the name obtained by a id.

I suppose that can i do with a inner join between users and scores.

How can i do that?

Insert into scores(id, name, score)
    select ID, name, score(that you can pass)
    from users
    where id = (parameter pass by PHP)

Agree. You can simply use this:

INSERT INTO scores values (null, (select name from users where users.id= 1),100);

Replace the score and id with the values you get. Assume you have the auto increment for your ids.

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