2017-11-03 5 views
0

같은 :CONCAT 두 개의 문자열 내가</p> <pre><code>table payment_enum( id int PK, name text, category_id int FK to category ) table category( id int PK, category_name text ) </code></pre> <p>내가 원하는 것은 MySQL의에서 다음과 같은 테이블이다 한 FK

: 그 추적 할 수없는 가망이 차 이름이 있지만, 예를 들어 같은 CATEGORY_ID 두 개의 열을 가지고 payment_enum에
select * from payment_enum 
pk name category_id 
1 'first' 2 
2 'second' 2 

그리고 PK와 분류 = 2가 이름을 '노트북' 이 가능 다음과 같은 결과

를 생성합니다 쿼리를 사용할 수 있나요

의사 쿼리

select c.name,c.pk, e.name as (concat (e.name,concat(',',select e2.name where e2.name like e.name from payment_enum as e2))) 
from category as c and payment_enum as e 

답변

0

concat의하면이 시도 :

select c.name as category_name ,c.pk as category_PK , GROUP_CONCAT(p.name) as enum_names from 
(select * from category) as c 
left JOIN 
(select * from payment_enum) as p 
on c.id = p.category_id GROUP BY c.name,c.pk 
0

당신이 필요로하는 group_concat 대신

select c.name,group_concat(e.name) as enum_names 
from category as c 
left join payment_enum as e on c.id = e.category_id 
group by c.name