如何加入以下两个视图?我自己尝试过,但它们总是会中断,并显示重复的数据。我能想到的唯一解决方法是创建两个视图,然后创建第三个视图来加入这两个视图。问题是,对于我的应用程序,我将拥有大约 10 个视图,这既不高效也不正确设计。
查看 1
SELECT e.id, e.name, GROUP_CONCAT(email SEPARATOR ', ') AS array_emails
FROM entity AS e
LEFT JOIN emails ON e.id=emails.entity_id
GROUP BY id;
查看 2
SELECT e.id, e.name, GROUP_CONCAT(web_site SEPARATOR ', ') AS array_web_sites
FROM entity AS e
LEFT JOIN web_sites ON e.id=web_sites.entity_id
GROUP BY id
视图 3(此视图将视图 1 和 2 连接在一起)
SELECT e.id, e.name, view_web_sites.array_web_sites, view_emails.array_emails
FROM entity AS e
LEFT JOIN view_web_sites ON e.id=view_web_sites.id
LEFT JOIN view_emails ON e.id=view_emails.id
GROUP BY id
这是我原来的观点,没有正常工作。这些值会重复。
原始 - 损坏的视图 SELECT e.id, e.name, GROUP_CONCAT(email SEPARATOR ',') AS emails, GROUP_CONCAT(web_site SEPARATOR ',') AS web_sites FROM entity AS e LEFT JOIN emails ON e.id=emails.entity_id LEFT在 e.id=web_sites.entity_id GROUP BY id 上加入网站;
编辑:我已经探索了 select 语句中的不同参数,效果很好。但是,在一个表中,我有一个二进制列,并且 distinct 参数“擦除”该值,使该列无用。关于这个问题有什么建议吗?
编辑#2(FIX):我设法为任何可能面临类似问题的人解决了这个问题。
SELECT entity.id, entity.name,
(SELECT GROUP_CONCAT(emails.email) FROM emails WHERE entity.id=emails.entity_id) AS emails_array,
(SELECT GROUP_CONCAT(web_sites.web_site) FROM web_sites WHERE
entity.id=web_sites.entity_id) AS websites_array
FROM entity
ORDER BY id