我在SQL查询中包含Group_Concat函数时遇到问题。我试图用PHP来解决它,但我宁愿用SQL来改进它。我的代码如下,我使用的是最新版本的MYSQL。
SELECT Main.Title, Main.DatePrint, Main.PerformanceDate, Main.Unsure_Sure, Main.Register, Main.notes,
VenueKey.Venue,AuthorKey.Author, CompanyKey.Company, PrintKey.Printer, SourceKey.Source
FROM Main INNER JOIN MainVenue
ON Main.MainID = MainVenue.ID_V
INNER JOIN VenueKey
ON MainVenue.VenueID = VenueKey.ID_V_K
INNER JOIN MainAuthor
ON Main.MainID = MainAuthor.ID_A
INNER JOIN AuthorKey
ON MainAuthor.AuthorID = AuthorKey.ID_A_K
INNER JOIN MainCompany
ON Main.MainID = MainCompany.ID_C
INNER JOIN CompanyKey
ON MainCompany.CompanyID = CompanyKey.ID_C_K
INNER JOIN MainPrinter
ON Main.MainID = MainPrinter.ID_P
INNER JOIN PrintKey
ON MainPrinter.PrinterID = PrintKey.ID_P_K
INNER JOIN MainSource
ON Main.MainID = MainSource.ID_S
INNER JOIN SourceKey
ON MainSource.SourceID = SourceKey.ID_S_K
理想情况下,我想在作者,场地和公司上使用Group_Concat,但我一直很难让它工作,而是返回一个空值。如果我有什么办法可以解决这个问题,请告诉我。
感谢。
答案 0 :(得分:1)
您必须对结果进行分组,以便GROUP_CONCAT能够使用。尝试以下方面:
SELECT
AuthorKey.Author,
GROUP_CONCAT(VenueKey.Venue) AS all_venues,
GROUP_CONCAT(CompanyKey.Company) AS all_companies
...
GROUP BY AuthorKey.Author
获取作者列表,例如场地和公司的连锁列表。
答案 1 :(得分:0)
这篇文章解决了我的问题。我还重新审视了我的SELECT
声明并进行了调整。