deucalion0
deucalion0

Reputation: 2440

How to insert something into a table when another table holds a specific value using MySQL

I have a database that consists of two tables, which look like this:

users


userID (PK) | email | password |


photos


photoID (PK) | userID (FK) | directory|


I want to insert a directory path into directory where the userID in users is say 1.

I was checking out SQL on the w3schools but I didn't know what this type of query is called.

Can anyone suggest the SQL I need to accomplish this query?

Upvotes: 0

Views: 63

Answers (2)

Basti
Basti

Reputation: 4042

INSERT INTO photos (userID, directory) VALUES("1", "<your directory>");

or if you actually want to change the directory

UPDATE photos SET directory = "<your directory>" WHERE userId = "1";

Upvotes: 3

lamplightdev
lamplightdev

Reputation: 2081

Try something along these lines:

INSERT INTO photos SELECT 0, userID, 'directory_path' FROM users WHERE userID = 1

Upvotes: -1

Related Questions