带有join的MySQL group_concat

时间:2014-12-29 11:06:32

标签: mysql sql select left-join group-concat

我有联系表。每个联系人可以有多个具有不同或相同表的电话号码。同样,每个联系人可以有多个具有不同或相同标签的电子邮件。我需要选择所有不同的电话号码和电子邮件ID属于特定联系人。

CREATE TABLE IF NOT EXISTS `contact` (
  `id` int(8) unsigned NOT NULL ,
  `fname` varchar(64)  NOT NULL,
  `lname` varchar(64)  NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1  ;
CREATE TABLE IF NOT EXISTS `phone` (
  `id` int(8) unsigned NOT NULL ,
  `sourceid` int(8) unsigned NOT NULL ,
   `type` varchar(16)  NOT NULL,
  `phone` varchar(16)  NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1  ;
CREATE TABLE IF NOT EXISTS `email` (
  `id` int(8) unsigned NOT NULL ,
  `sourceid` int(8) unsigned NOT NULL ,
  `type` varchar(16)  NOT NULL,
  `email` varchar(128)  NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1  ;

INSERT INTO contact values ( 1,'john'      ,'j' ),
(2, 'jose'      ,'f' ),
(3, 'test'      ,'k' ),
(4, 'tester'      ,'j' );


INSERT INTO phone values( 1 ,1, 'Home', '123456' ),
( 2 ,1, 'Home', '123456342' ),
( 3 ,1, 'Office', '12345645' ),
( 4 ,1, 'Mobile', '1234567' ),
( 5 ,2, 'Home', '123456' ),
( 5 ,2, 'Home', '987556' );

INSERT INTO email values
( 1 ,1, 'Home', 'john@gmail.com' ),
( 2 ,1, 'Home', 'john@yahoo.com' ),
( 3 ,1, 'Office', 'john@inc.com' ),
( 4 ,2, 'Home', 'jose@gmail.com' ),
( 5 ,4, 'Home', 'test@test.com' );

我尝试过GROUP_CONCAT,但标签上有重复的条目。

SELECT C.id, fname, lname
, GROUP_CONCAT(ph.sourceid), 
GROUP_CONCAT(em.sourceid), 
GROUP_CONCAT(ph.type), GROUP_CONCAT(ph.phone),
GROUP_CONCAT(em.type), GROUP_CONCAT(em.email)
FROM contact AS C 
LEFT  JOIN phone AS PH ON PH.sourceid = C.id 
LEFT  JOIN email EM ON EM.sourceid = C.id 
WHERE C.id='1'
GROUP BY C.id

SQL FIDDLE

1 个答案:

答案 0 :(得分:3)

试试这个:

SELECT c.id, c.fname, c.lname, 
       ph.PhoneType, ph.PhoneNos, 
       em.EmailType, em.EmailIds
FROM contact AS c 
LEFT JOIN (SELECT ph.sourceid, GROUP_CONCAT(ph.type) AS PhoneType, GROUP_CONCAT(ph.phone) AS PhoneNos 
           FROM phone AS ph 
           GROUP BY ph.sourceid
          ) AS ph ON ph.sourceid = c.id 
LEFT JOIN (SELECT em.sourceid, GROUP_CONCAT(em.type) AS EmailType, GROUP_CONCAT(em.email) AS EmailIds
           FROM email AS em 
           GROUP BY em.sourceid
          ) AS em ON em.sourceid = c.id 
WHERE c.id = '1'
GROUP BY c.id

检查SQL FIDDLE DEMO

<强>输出

| ID | FNAME | LNAME |               PHONETYPE |                          PHONENOS |        EMAILTYPE |                                   EMAILIDS |
|----|-------|-------|-------------------------|-----------------------------------|------------------|--------------------------------------------|
|  1 |  john |     j | Home,Home,Office,Mobile | 123456,123456342,12345645,1234567 | Home,Home,Office | john@gmail.com,john@yahoo.com,john@inc.com |