다음 테이블 구조가 제공됩니다.
1 번 테이블
first | pkey <- column names
val1 | 1
표 2
second | fkey <- column names
val2 | 1
val3 | 1
val4 | 1
다음과 같은 출력을 원합니다.
first | second <- column names
val1 | val2, val3, val4
나는 시도했다 :
select table1.first, (select table2.second where table1.pkey=table2.fkey)
from table1 join on table2 where table1.pkey=table2.fkey;
그것에 대해 뭔가 잘못 보이므로 다음과 같이 표시됩니다.
more than one row returned by a subquery used as an expression
나는 또한 시도했다
select table1.first, (select table2.second where table1.pkey=table2.fkey)
from table1, table2;
을 사용하여 다음을 시도하십시오 string_agg
. 여기 데모입니다
select
first,
string_agg(second, ',') as second
from table1 t1
join table2 t2
on t1.pkey = t2.fkey
group by
first
산출
*----------------------*
| first second |
*----------------------*
| val1 val2,val3,val4 |
*----------------------*
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다