SQL查询:组合两个表以在另一个表中插入数据

时间:2017-12-13 15:21:21

标签: sql postgresql

所以,我有这三个表,我想在某个地址插入一个新事件,但我不知道该怎么做。

account(id, email, password)
address(id, city, street, number, c_id(foreign key for account))
event(id, title, description, e_id(foreign key for address))

这个想法是用户可以在自己的地址注册一个事件。贝娄我想做什么。

/*I believe this can return multiple lines.*/
SELECT c_id FROM public.address WHERE city='new york';

/*This will return only one line.*/
SELECT id FROM public.account WHERE email='user01@gmail.com' AND password='user01';

事件中的插入类似于:

/*I don't know how to combine those lines above.*/
INSERT INTO public.event(id, title, description, c_id, e_id) VALUES ('1', 'event title', 'event description', '????')

1 个答案:

答案 0 :(得分:-1)

在地址表中,您可以将city设为主键,以便您可以将其作为外键添加到事件表中。