0

ユーザーの3つの電話番号を1つの行にまとめる必要があります。最終結果は次のようになります

contactId | 電話| モバイル| ファックス

私のテーブルはこのように見え、電話番号はphoneTypeIdによって独自の行に分割されています


contact_phone_link
contactId | contactPhoneNumberId
3344 | 1


contact_phone_numbers
id | phoneTypeId | 電話番号
123| 1 | 555-555-5555


phone_types
id | phoneTypeName
1 | 電話
2| モバイル
3| ファックス


ダナはあなたの助けに感謝します、私はこれを理解することに近づいています。グループ化と命名、そして再びグループ化する方法がわかります。

構文を確認して調整しましたが、「phone_link.phoneTypeId」に到達する際にまだ問題があります。'contact_phone_link'が'phoneTypeId'を保持していないためだと思います。また、SELECTセクションとJOINセクションが「phone」のように同じ変数名を持っているとハングアップするので、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

sのテーブルを作成し、contactIdそれに数値を結合する必要があります。

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 に答える