您好,我在尋找解決方案時遇到了問題。複雜的SQL查詢::從乘法表內聯顯示數據
我有3個表:
- 客戶
- CUSTOMER_ID
- CUSTOMER_NAME
- 關係
- subscription_id
- CUSTOMER_ID
- 訂閱
一個客戶可以有許多訂閱關係,我想,像這樣顯示的數據:
customer_id, customer_name, subscription_first, subscription_second, subscription_n
...所有在一行。
任何人都可以幫助我嗎?
好吧,首先,感謝您的幫助:)
我做了這樣的事情,其工作:)
SELECT `main_table`.*, `customer_lastname_table`.`value` AS `customer_lastname`, `customer_firstname_table`.`value` AS `customer_firstname`, IF(main_table.customer_id = 0, 1, 2) AS `type`, `store`.`group_id`, `store`.`website_id`, `subscription_table_one`.`subscription_code`, `subscription_table_two`.`subscription_code` FROM `newsletter_subscriber` AS `main_table`
LEFT JOIN `customer_entity_varchar` AS `customer_lastname_table` ON customer_lastname_table.entity_id=main_table.customer_id
AND customer_lastname_table.attribute_id = 7
LEFT JOIN `customer_entity_varchar` AS `customer_firstname_table` ON customer_firstname_table.entity_id=main_table.customer_id
AND customer_firstname_table.attribute_id = 5
INNER JOIN `core_store` AS `store` ON store.store_id = main_table.store_id
LEFT JOIN `b_newsletter_relations` AS `relation_table` ON relation_table.customer_id=main_table.customer_id
LEFT JOIN `b_newsletter_subscriptions` AS `subscription_table_one` ON subscription_table_one.subscription_id=relation_table.subscription_id
LEFT JOIN `b_newsletter_subscriptions` AS `subscription_table_two` ON subscription_table_one.subscription_id=relation_table.subscription_id
GROUP BY `customer_id`
AAA,還有一兩件事,它通過PHP的動態。
你怎麼知道哪一個是1st/2nd/3rd/etc訂閱?是否有每個客戶的限制,否則這必須是動態的,以適應訂閱最多的客戶... – 2010-07-23 18:56:21