我在按用户给出的参数对存储过程进行排序时遇到问题,我尝试过阅读,但我尝试过的解决方案不起作用。
那么,有人可以帮助我吗?我希望它按类型从我的 Group_concat 中排序。如果我用硬编码输入它,它就可以工作,比如(ORDER BY Type1,ORDER BY Type2 等等)。但我想要这样的东西(ORDER BY @specificStat)。
CREATE PROCEDURE getSpecificStatsBySeason(IN khID INT(11), IN seasonName varchar(40), IN specificStat INT(11))
BEGIN
SET @sql = NULL;
SET @khID = khID;
SET @seasonName = seasonName;
SET @specificStat = specificStat;
SELECT
GROUP_CONCAT(DISTINCT CONCAT('SUM(IF(em.eventTypeID = ',
et.eventTypeID, ', 1, 0)) AS Type',
et.eventTypeID )
) INTO @sql
FROM eventTypes AS et, `eventType-R-kh` AS eRk, kh AS kh WHERE et.eventTypeID = eRk.eventTypeID AND eRk.kh = kh.kh AND kh.khID = @khID;
SET @sql = CONCAT('SELECT u.userID, ud.firstname, ud.lastname,', @sql,
',
IFNULL((SELECT COUNT(s.activityID)
FROM activity AS a1, signup AS s
WHERE u.userid = s.userid
AND s.activityID = a1.activityID
AND a1.khID = @khID
AND s.answer = 1
AND se.seasonName = @seasonName
AND a1.activityDate BETWEEN se.seasonStart AND se.seasonEnd
AND a1.activityDate <= NOW()), 0) AS matchCount
FROM userData AS ud, userInfo AS u
LEFT JOIN activity AS a ON a.khID = @khID
RIGHT JOIN season AS se ON se.seasonName = @seasonName
AND a.activityDate BETWEEN se.seasonStart AND se.seasonEnd
LEFT JOIN `eventMatch` AS em ON em.userid = u.userid
AND em.activityID = a.activityID
LEFT JOIN activityMatch AS am ON a.activityID = am.activityID
WHERE u.khID = @khID
AND u.userID = ud.userID
GROUP BY u.UserID
ORDER BY @specificStat ;');
prepare stmt
FROM @sql;
execute stmt;