これは複雑な質問ですので、ご容赦ください。3つの異なるテーブルを使用して1つの結果セットを作成しています。それらは次のとおりです。
customer_address_entity
entity_id | entity_type_id | attribute_set_id | increment_id | parent_id | create_at | update_at | is_active
customer_entity_int
value_id | entity_type_id | attribute_id | entity_id | value
customer_address_entity_varchar
value_id | entity_type_id | attribute_id | entity_id | value
これで構造ができました。これまでに作成したSQL呼び出しは次のとおりです。
SELECT CAE.entity_id,
CEI.value AS default_entity_id,
CAEV.attribute_id,
CAEV.value
FROM customer_address_entity AS CAE
JOIN customer_entity_int AS CEI
ON CEI.entity_id = CAE.parent_id
AND CEI.attribute_id = '13'
JOIN customer_address_entity_varchar AS CAEV
ON CAEV.entity_id = CAE.entity_id
WHERE CAE.parent_id = '2328'
AND CAE.is_active = 1
これにより、次のサンプルデータセットが出力されます。
ID default att value
'1567', '1567', '19', 'John'
'1567', '1567', '21', 'Doe'
'1567', '1567', '23', 'Johns Company'
'1567', '1567', '25', 'Johns City'
'1567', '1567', '26', 'Johns Country'
'1567', '1567', '27', 'Johns State'
'1567', '1567', '29', 'Johns Zip Code'
'1567', '1567', '30', 'Johns Phone'
'1567', '1567', '31', 'Johns Fax'
'1568', '1567', '19', 'Jane'
'1568', '1567', '21', 'Doe'
'1568', '1567', '23', 'Janes Company'
'1568', '1567', '25', 'Janes City'
'1568', '1567', '26', 'Janes Country'
'1568', '1567', '27', 'Janes State'
'1568', '1567', '29', 'Janes Zip'
'1568', '1567', '30', 'Janes Phone'
'1568', '1567', '31', 'Janes Fax'
'1569', '1567', '19', 'Frank'
'1569', '1567', '21', 'Frunz'
'1569', '1567', '23', 'Franks Company'
'1569', '1567', '25', 'Franks City'
'1569', '1567', '26', 'Franks Country'
'1569', '1567', '27', 'Franks State'
'1569', '1567', '29', 'Franks Zip'
'1569', '1567', '30', 'Franks Phone'
'1569', '1567', '31', 'Franks Fax'
このコードの最後の部分では、番号UNIQUE
entity_id
(返されたデータセットの列1、つまり.1567、1568、1569)に基づいてROWSのX番号(この場合は3)を作成します。意図した最終結果は次のとおりです。
'1567', '1567', 'John', 'Doe', 'Johns Company', 'Johns City', 'Johns State', 'Johns Zip Code', 'Johns Phone', 'Johns Fax'
'1568', '1567', 'Jane', 'Doe', 'Janes Company', ... etc
'1569', '1567', 'Frank', 'Franz', 'Franks Comapny', ... etc
これも可能ですか?
ゴードン・リノフに感謝します-答えはエレガントでシンプルです!私は自分の編集をいくつか投げましたが、ゴードンの答えを受け入れて投票します。これが私が行った編集で、美しく機能します!!
select entity_id,
if(entity_id = default_entity_id, 'true', 'false') as default_entity,
max(case when attr = '19' then `value` end) as `FirstName`,
max(case when attr = '21' then `value` end) as `LastName`,
max(case when attr = '23' then `value` end) as `CompanyName`,
max(case when attr = '25' then `value` end) as `City`,
max(case when attr = '27' then `value` end) as `State`,
max(case when attr = '29' then `value` end) as `ZipCode`,
max(case when attr = '30' then `value` end) as `PhoneNumber`,
max(case when attr = '31' then `value` end) as `Fax`
from (SELECT CAE.entity_id, CEI.value AS default_entity_id, CAEV.attribute_id AS attr, CAEV.value
FROM customer_address_entity CAE
JOIN customer_entity_int CEI
ON CEI.entity_id = CAE.parent_id
AND CEI.attribute_id = '13'
JOIN customer_address_entity_varchar CAEV
ON CAEV.entity_id = CAE.entity_id
WHERE CAE.parent_id = '2328'
AND CAE.is_active = 1
) as t
group by entity_id