连接来自旋转 mysql 的结果值
concat result values from pivoting mysql
我试图在旋转后获得一个连接的字符串值。
目前,只有一个值并且工作正常。
查询如下:
DROP PROCEDURE IF EXISTS getAllUserLunchReport;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getAllUserLunchReport`(IN `start_date` DATETIME, IN `end_date` DATETIME)
BEGIN
SET GLOBAL group_concat_max_len=4294967295;
SET @SQL = NULL;
SET @start_date = DATE(start_date);
SET @end_date = DATE(end_date);
SELECT
COALESCE(GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN date = "',DATE(issuedDateTime),'" AND lunchStatus = 1 THEN (SELECT CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), "," ,(SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))) ELSE 0 END) AS `',DATE(issuedDateTime),'`'
) ORDER BY issuedDateTime
), '0 as `NoMatchingRows`') INTO @SQL
FROM `lunch_status`
WHERE DATE(issuedDateTime) BETWEEN @start_date AND @end_date;
SET @SQL
= CONCAT
(
'
SELECT concat(u.firstname," ",u.lastname) as Employee, w.*
FROM users u
INNER JOIN
(SELECT userId, ', @SQL, '
FROM
(
SELECT userId, lunchStatus, DATE(issuedDateTime) as date
FROM `lunch_status`
WHERE DATE(issuedDateTime) BETWEEN "',@start_date,'" AND "',@end_date,'"
) as a
GROUP BY userId) w
ON u.id = w.userId
ORDER BY Employee;
'
);
PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
我只得到第一个值,没有我试图连接的其他值。
这是我预期的结果:
employee | 2019-1-15 | 2019-1-16
----------------------------------------
Jack | 30,140 | 30,140
虽然单独执行串联查询是可行的。
注意:在上面的存储过程中,如果分隔符被移除,值将被连接起来。 CONCAT((select ...), (select...))
给出 30140
.
会不会是quotes
的问题,我已经试过了
仅供参考,这是 @SQL
第一次查询后的结果:
SUM(CASE WHEN date = "2019-01-15" AND lunchStatus = 1 THEN (SELECT CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), ",", (SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))) ELSE 0 END) AS `2019-01-15`,
SUM(CASE WHEN date = "2019-01-16" AND lunchStatus = 1 THEN (SELECT CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), ",", (SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))) ELSE 0 END) AS `2019-01-16`
看起来有效。
编辑:它似乎在 "integer" 值下工作正常。
例如
CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), "111" ,(SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))
给出 301140
.
但是对于其他字符,/./-/_
,只显示第一个参数:30
.
当您在 CONCAT
中包含 ,
时,您的代码不起作用的原因是因为 CONCAT
在 SUM
中,它赢了' 能够将 30,140
解释为一个数字,它可以将 30140
解释为一个数字(因此没有分隔符你会得到一个结果)。在不知道 table 数据的更多细节和期望的结果的情况下,很难准确地说出如何解决问题,但也许您想要这样的东西:
CASE WHEN date = "2019-01-15" AND lunchStatus = 1 THEN CONCAT((SELECT SUM(rate) FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), ",", (SELECT SUM(rate) FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1)) ELSE 0 END) AS `2019-01-15`
我试图在旋转后获得一个连接的字符串值。
目前,只有一个值并且工作正常。
查询如下:
DROP PROCEDURE IF EXISTS getAllUserLunchReport;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `getAllUserLunchReport`(IN `start_date` DATETIME, IN `end_date` DATETIME)
BEGIN
SET GLOBAL group_concat_max_len=4294967295;
SET @SQL = NULL;
SET @start_date = DATE(start_date);
SET @end_date = DATE(end_date);
SELECT
COALESCE(GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN date = "',DATE(issuedDateTime),'" AND lunchStatus = 1 THEN (SELECT CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), "," ,(SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))) ELSE 0 END) AS `',DATE(issuedDateTime),'`'
) ORDER BY issuedDateTime
), '0 as `NoMatchingRows`') INTO @SQL
FROM `lunch_status`
WHERE DATE(issuedDateTime) BETWEEN @start_date AND @end_date;
SET @SQL
= CONCAT
(
'
SELECT concat(u.firstname," ",u.lastname) as Employee, w.*
FROM users u
INNER JOIN
(SELECT userId, ', @SQL, '
FROM
(
SELECT userId, lunchStatus, DATE(issuedDateTime) as date
FROM `lunch_status`
WHERE DATE(issuedDateTime) BETWEEN "',@start_date,'" AND "',@end_date,'"
) as a
GROUP BY userId) w
ON u.id = w.userId
ORDER BY Employee;
'
);
PREPARE stmt FROM @SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
我只得到第一个值,没有我试图连接的其他值。
这是我预期的结果:
employee | 2019-1-15 | 2019-1-16
----------------------------------------
Jack | 30,140 | 30,140
虽然单独执行串联查询是可行的。
注意:在上面的存储过程中,如果分隔符被移除,值将被连接起来。 CONCAT((select ...), (select...))
给出 30140
.
会不会是quotes
的问题,我已经试过了
仅供参考,这是 @SQL
第一次查询后的结果:
SUM(CASE WHEN date = "2019-01-15" AND lunchStatus = 1 THEN (SELECT CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), ",", (SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))) ELSE 0 END) AS `2019-01-15`,
SUM(CASE WHEN date = "2019-01-16" AND lunchStatus = 1 THEN (SELECT CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), ",", (SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))) ELSE 0 END) AS `2019-01-16`
看起来有效。
编辑:它似乎在 "integer" 值下工作正常。 例如
CONCAT((SELECT rate FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), "111" ,(SELECT rate FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1))
给出 301140
.
但是对于其他字符,/./-/_
,只显示第一个参数:30
.
当您在 CONCAT
中包含 ,
时,您的代码不起作用的原因是因为 CONCAT
在 SUM
中,它赢了' 能够将 30,140
解释为一个数字,它可以将 30140
解释为一个数字(因此没有分隔符你会得到一个结果)。在不知道 table 数据的更多细节和期望的结果的情况下,很难准确地说出如何解决问题,但也许您想要这样的东西:
CASE WHEN date = "2019-01-15" AND lunchStatus = 1 THEN CONCAT((SELECT SUM(rate) FROM lunch_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1), ",", (SELECT SUM(rate) FROM lunch_gbd_rate WHERE DATE(created_on) <= date ORDER BY created_on DESC LIMIT 1)) ELSE 0 END) AS `2019-01-15`