I have a database like where:
Table foo
has columns id
and name
Table bar
has columns id
and foo_id
I have an incoming HTTP query with a foo.name
, I'd like to insert a row into bar
with bar.foo_id
set appropriately. So, for example:
> SELECT * FROM foo;
id name
------ -------
1 "Andrey"
(1 row)
> SELECT * FROM bar;
(0 rows)
Given "Andrey"
, is there a single query I can execute to get:
> SELECT * FROM bar;
id foo_id
------ -------
1 1
(1 row)
I was thinking along the lines of:
> UPDATE bar SET foo_id=(SELECT id FROM foo WHERE foo.name=?)
But this seems to be wrong, as SELECT's return sets, not values...