SQL Fiddle providing tables and current proc
我有两个表 - 父表和包含元数据的表。我有一个存储过程(我发誓在某一点上工作),它将元表中基于行的数据转换为列,并使用左连接将父表与行数据合并。我现在收到错误1064。
更新:我在proc中输出查询以进行调试,我发现最后一行到列pivot永远不会提供列名(从metakey值中提取)。最后一栏并不重要,它总是在n个字符后被切断(在截断之前看起来与多少个字符不一致)!这是查询的完整输出。
SELECT q.*,
MAX(IF(m.metakey = 'account_number', m.metavalue, NULL)) AS `account_number`,MAX(IF(m.metakey = 'area', m.metavalue, NULL)) AS `area`,MAX(IF(m.metakey = 'attachment_name', m.metavalue, NULL)) AS `attachment_name`,MAX(IF(m.metakey = 'attachment_token', m.metavalue, NULL)) AS `attachment_token`,MAX(IF(m.metakey = 'attachment_url', m.metavalue, NULL)) AS `attachment_url`,MAX(IF(m.metakey = 'description', m.metavalue, NULL)) AS `description`,MAX(IF(m.metakey = 'device_name', m.metavalue, NULL)) AS `device_name`,MAX(IF(m.metakey = 'email', m.metavalue, NULL)) AS `email`,MAX(IF(m.metakey = 'engine_files_attachment_name', m.metavalue, NULL)) AS `engine_files_attachment_name`,MAX(IF(m.metakey = 'engine_files_attachment_token', m.metavalue, NULL)) AS `engine_files_attachment_token`,MAX(IF(m.metakey = 'engine_files_attachment_url', m.metavalue, NULL)) AS `engine_files_attachment_url`,MAX(IF(m.metakey = 'fname_lname', m.metavalue, NULL)) AS `fname_lname`,
/** THIS IS WHERE THE ISSUE OCCURS **/
MAX(IF(m.metakey = 'how_connected_network', m.metavalue, NULL)) AS ,
m.json
FROM support_ticket_queue q LEFT JOIN support_ticket_queue_meta m ON q.id = m.ticket_queue_id GROUP BY q.id
proc:
DELIMITER $$
CREATE PROCEDURE `p_support_ticket_queue`()
BEGIN
SET @sql = NULL;
SELECT
GROUP_CONCAT(
DISTINCT CONCAT(
'MAX(IF(m.metakey = ''',m.metakey,''', m.metavalue, NULL)) AS ','`',m.metakey,'`'
)
ORDER BY m.metakey
)
INTO @sql FROM
support_ticket_queue_meta m;
SET @sql = CONCAT ( 'SELECT q.*,' , @sql , ' , m.json FROM support_ticket_queue q LEFT JOIN support_ticket_queue_meta m ON q.id = m.ticket_queue_id GROUP BY q.id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
END$$
DELIMITER ;
调用此proc会在响应中返回以下错误:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM support_ticket_queue q LEFT JOIN support_ticket_queue_meta m ON q.id = m.ti' at line 1
两个表的概述信息如下:
support_ticket_queue (the parent table)
---------------------------------------
id (primary auto increment)
vertical (varchar)
created_on (datetime)
updated_on (datetime)
support_ticket_queue_meta
---------------------------------------
id (primary auto incremenet)
ticket_queue_id (foreign key to id from parent table)
metakey (varchar)
metavalue (text)
答案 0 :(得分:0)
进行了一些调试测试并注意到我的GROUP_CONCAT模式在1024个字符后被截断。进一步在线搜索并发现GROUP_CONCAT实际上有1024个长度限制。
通过将group_concat_max_len值增加到更高的限制已解决了我的问题。
SET SESSION group_concat_max_len = 2500;