我需要将用户的 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);