gpt4 book ai didi

mysql - 按存储过程参数排序

转载 作者:行者123 更新时间:2023-11-29 02:29:00 28 4
gpt4 key购买 nike

我在按用户给定的参数对我的存储过程进行排序时遇到问题,我已尝试阅读,但我尝试过的解决方案不起作用。

那么,有没有人可以帮助我?我希望它从我的 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;

最佳答案

改用这个:

...
...
AND u.userID = ud.userID
GROUP BY u.UserID
ORDER BY ', @specificStat, ' ;');

关于mysql - 按存储过程参数排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15876263/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com