0

我需要将用户的 3 个电话号码组合成一行。最终结果看起来像这样

联系方式 | 电话 | 移动 | 传真

我的表格看起来像这样,电话号码按 phoneTypeId 分成自己的行


contact_phone_link
联系人 ID | 联系电话号码Id
3344 | 1


联系人电话号码
ID | 电话类型ID | 电话号码
123 | 1 | 555-555-5555


phone_types
id | 电话类型名称
1 | 电话
2 | 手机
3 | 传真


达纳感谢您的帮助,我越来越了解这一点。我看到它是如何分组和命名然后再次分组的。

我已经完成并调整了语法,但我仍然遇到“phone_link.phoneTypeId”的问题。我认为这是因为“contact_phone_link”不包含“phoneTypeId”。当 SELECT 部分和 JOIN 部分具有相同的变量名称(如“电话”)时,它也会挂起,所以我添加了 1。

告诉我,如果我离开了。

SELECT
    `ct`.`id` AS contact_id,
    `phone1`.`lineNumber` AS phone,
    `fax1`.`lineNumber` AS fax,
    `mobile1`.`lineNumber` AS mobile

FROM `cmd`.`contacts` AS ct

LEFT JOIN `cmd`.`contact_phone_link` AS `phone_link` ON (`phone_link`.`contactId` = `ct`.`id` AND `phone_link`.`phoneTypeId` = 1)
LEFT JOIN `cmd`.`contact_phone_numbers` AS `phone1` ON (`phone`.`id` = `phone_link`.`contactPhoneNumberId`)

LEFT JOIN `cmd`.`contact_phone_link` AS `fax_link` ON (`fax_link`.`contactId` = `ct`.`id` AND `fax_link`.`phoneTypeId` = 2)
LEFT JOIN `cmd`.`contact_phone_numbers` AS `fax1` ON (`fax`.`id` = `fax_link`.`contactPhoneNumberId`)

LEFT JOIN `cmd`.`contact_phone_link` AS `mobile_link` ON (`mobile_link`.`contactId` = `ct`.`id` AND `mobile_link`.`phoneTypeId` = 3)
LEFT JOIN `cmd`.`contact_phone_numbers` AS `mobile1` ON (`mobile`.`id` = `mobile_link`.`contactPhoneNumberId`)

WHERE (`ct`.`id` = 4160);
4

2 回答 2

0

我们需要创建一个contactIds 表并将数字加入其中:

SELECT
  idlist.contactId AS contactId,
  phones.phone AS phone,
  mobiles.mobile AS mobile,
  faxes.fax AS fax
FROM
  -- This builds the ID list
  (SELECT DISTINCT contactId
   FROM contact_phone_link
  ) AS idlist
LEFT JOIN
  -- This gets the landlines
  (SELECT
     contact_phone_link.contactId AS contactId,
     contact_phone_numbers.phoneNumber AS phone
   FROM contact_phone_link
     INNER JOIN contact_phone_numbers ON contact_phone_numbers.id=contact_phone_link.contactPhoneNumberId
   WHERE contact_phone_numbers.phoneTypeId=1
  ) AS phones ON phones.contactId=idlist.contactIs
LEFT JOIN
  -- This gets the mobiles
  (SELECT
     contact_phone_link.contactId AS contactId,
     contact_phone_numbers.phoneNumber AS mobile
   FROM contact_phone_link
     INNER JOIN contact_phone_numbers ON contact_phone_numbers.id=contact_phone_link.contactPhoneNumberId
   WHERE contact_phone_numbers.phoneTypeId=2
  ) AS mobiles ON mobiles.contactId=idlist.contactId
LEFT JOIN
  -- This gets the faxes
  (SELECT
     contact_phone_link.contactId AS contactId,
     contact_phone_numbers.phoneNumber AS fax
   FROM contact_phone_link
     INNER JOIN contact_phone_numbers ON contact_phone_numbers.id=contact_phone_link.contactPhoneNumberId
   WHERE contact_phone_numbers.phoneTypeId=3
  ) AS faxes ON faxes.contactId=idlist.contactId
于 2012-08-20T17:14:09.363 回答
0

下面编辑解决方案。

SELECT
    `ct`.`id` AS contact_id,
    `phone_sub_query`.`lineNumber` AS phone,
    `fax_sub_query`.`lineNumber` AS fax,
    `mobile_sub_query`.`lineNumber` AS mobile

FROM `cmd`.`contacts` AS ct

left join
    (select `phone_link`.`contactId`, `phone_number`.`lineNumber`
    from `cmd`.`contact_phone_link` AS `phone_link`
    join `cmd`.`contact_phone_numbers` AS `phone_number` ON (`phone_number`.`id` = `phone_link`.`contactPhoneNumberId`)
    where `phone_number`.`phoneTypeId` = 1) as `phone_sub_query` on `phone_sub_query`.`contactId` = `ct`.`id`

left join
    (select `phone_link`.`contactId`, `phone_number`.`lineNumber`
    from `cmd`.`contact_phone_link` AS `phone_link`
    join `cmd`.`contact_phone_numbers` AS `phone_number` ON (`phone_number`.`id` = `phone_link`.`contactPhoneNumberId`)
    where `phone_number`.`phoneTypeId` = 2) as `fax_sub_query` on `fax_sub_query`.`contactId` = `ct`.`id`

left join
    (select `phone_link`.`contactId`, `phone_number`.`lineNumber`
    from `cmd`.`contact_phone_link` AS `phone_link`
    join `cmd`.`contact_phone_numbers` AS `phone_number` ON (`phone_number`.`id` = `phone_link`.`contactPhoneNumberId`)
    where `phone_number`.`phoneTypeId` = 3) as `mobile_sub_query` on `mobile_sub_query`.`contactId` = `ct`.`id`

WHERE (`ct`.`id` = 4160);

鉴于您的表结构,我认为可能需要使用子查询。我的解决方案实际上看起来更像@Eugene 现在的解决方案,但你想left join反对inner join. 我的子查询检查电话类型并发出 (a) 联系人 ID 和 (b) 线路号码。

让我知道这个是否奏效。我认为它应该更接近。

于 2012-08-20T17:14:20.563 回答