PostgreSQL将两个表中的查询结果插入到新表中

时间:2017-03-18 10:40:40

标签: sql postgresql

我有一个provided_services表:

id | user_id | supplied_service_name
1  | 1       | English Translations
2  | 2       | English Teaching
3  | 3       | English Teaching

我有一个needed_services表:

id | user_id | needed_service_name
1  | 4       | English Teaching
2  | 5       | English Translations
3  | 5       | English Teaching

有一个匹配表,应该与供应商匹配needers。我的SQL是({{3}}):

insert into matches (
    user_needer,
    user_supplier,
    needed_service_name,
    supplied_service_name
    )
select ns.user_id,
    ss.user_id,
    needed_service_name,
    supplied_service_name
from supplied_services ss
inner join needed_services ns on ss.supplied_service_name = ns.needed_service_name

输出应为:

id | user_needer | user_supplier | needed_service_name |supplied_service_name
1  | 1           | 5             | English Translations|English Translations
2  | 3           | 4             | English Teaching    | English Teaching
3  | 8           | 7             | English Teaching    | English Teaching
4  | 2           | 5             | English Teaching    | English Teaching
5  | 3           | 5             | English Teaching    | English Teaching

然而,输出是这样的:

id | user_needer | user_supplier | needed_service_name |supplied_service_name
1  | 1           | 5             | English Translations|English Translations
2  | 3           | 4             | English Teaching    | English Teaching
3  | 8           | 7             | English Teaching    | English Teaching

获得比赛后,它不会寻找任何其他比赛。如何修复SQL以便输出正确的表?

0 个答案:

没有答案