复杂的 SQL 查询 :: 显示来自多个表的内联数据
您好,我在寻找解决方案时遇到问题。
我有 3 张桌子:
- 顾客
- customer_id
- 客户名称
- 关系
- 订阅 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`
啊啊,还有一件事,它是通过 PHP 动态实现的。
Hi i have a proble in finding solution.
I have 3 tables:
- Customer
- customer_id
- customer_name
- Relation
- subscription_id
- customer_id
- Subscriptions
One customer can have relation with many subscriptions and i want to display data like so:
customer_id, customer_name, subscription_first, subscription_second, subscription_n
...all in one line.
Can any one help me :?
Ok, first of all, thanks for help :)
i did something like this and its working :)
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, and one more thing, it's dynamic via PHP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
MySQL
提供的方便的GROUP_CONCAT
函数。下面是解决方案。结果:
You can use the handy
GROUP_CONCAT
function available withMySQL
. Below is the solution.Results:
如果你能运用原则,这种情况就可以很容易地处理。然而,在这种情况下,学说本身会返回对象,如下所示,
如果您可以制作这样的数组,那么您进行这样的演示将非常简单。有人可以编写具有多级连接的复杂查询,这会导致更大的瓶颈情况。但我建议使用单个连接检索所有数据,并使用 php 引擎进行解析以获取此类数组并呈现数据。
谢谢
If you could use doctrine the this situation could be handled very easily. However doctrine itself returns object in such case alike as follows
if you can make such an array then it will be very simple for you to make such presentation. Someone can write a complex query with multi level joins it will let a bigger bottle nick situation. But I suggest retrieve all data with a single join and parse using php engine to get such array and present the data.
Thanks
你可以使用联盟。
比如
我根本不懂SQL,所以可能会有很多错误,但我认为它可以完成工作。
you can use union.
Something like
I don't know SQL at all, so there may be a lot of mistakes, but I think it does the job.