DeviArt
DeviArt

Reputation: 469

Creating a query to insert data into MySQL using foreign keys

I am a total newbie at DB's and I'm trying to insert data into the following tables:

Table Start

Table = users

Columns = id , username , password

Table = users_info

Columns = id , e-mail , address , logged , portrait , user_id

Table End

users_info.user_id is a foreign key linked to users.id.

I want to build a query that will insert data into (users_info table) based on information from (users table)... literal i.e.:

Insert portrait into users_info where user_id = users.id and username = JohnDoe

What is the syntax to get this going?

Thanks!!

Upvotes: 2

Views: 6366

Answers (2)

LoBS
LoBS

Reputation: 50

If what you want to do is update the data in users_info table then you have to use the UPDATE command.

UPDATE users_info a SET a.portrait = 'value1', a.logged = 'value2', a.address = 'value3', a.email = 'value4' where a.user_id = (SELECT DISTINCT(id) FROM users b WHERE b.username = 'JohnDoe')

Basics on SQL commands on http://www.w3schools.com/sql/

Good Luck!

Upvotes: 1

Pieter Geerkens
Pieter Geerkens

Reputation: 11883

Try this:

Insert portrait into users_info 
(id, email) VALUES ( select user.id, '[email protected]' )
where user_id = users.id and username = 'JohnDoe'
)

Upvotes: 0

Related Questions