我有两张桌子:
categories
=> Category_ID
,Title
,Description
,Default_Points
,Groups
transactions
=> Transaction_ID
,Datetime
,Giver_ID
,Recipient_ID
,Points
,Category_ID
,Reason
教师奖励积分,选择一个类别(如“积极态度和行为”)和一个理由(如“今日优秀作品”),将一个条目放入transactions
表。
典型的categories
行可能是:
INSERT INTO `categories` (`Category_ID`, `Title`, `Description`, `Default_Points`, `Groups`) VALUES
(17, 'Olympic Values', 'Please clearly state the correct Olympic Value that''s being used currently in the REASON box.', 5, '');
典型的transactions
行可能是:
INSERT INTO `transactions` (`Transaction_ID`, `Datetime`, `Giver_ID`, `Recipient_ID`, `Points`, `Category_ID`, `Reason`) VALUES
(50, '2011-09-07', 35023, 90236, 5, 17, 'Excellent work during PE');
我想尝试使用MySQL做的是生成一个总点数列表(即每个类别SUM(transactions.Points)
,还有一些样本Reasons
。
我想这必须使用CONCAT?
我需要:
SUM(transactions.Points)
categories.title
transactions.reason
这可能看起来像......
Points Title Sample
14252 Olympic Values Excellent work in PE!|Great display of friendship|Well done!
15532 Outstanding Effort Amazing work!|Worked so hard|Great piece!
这可能吗?
提前致谢,
答案 0 :(得分:1)
这是你想要的GROUP_CONCAT
。
你需要做这样的事情:
SELECT SUM(t.Points),
c.title,
SUBSTRING_INDEX(GROUP_CONCAT(transactions.reasons SEPERATOR '|'), '|', 5)
FROM transactions t JOIN categories c ON (t.Category_ID=c.Category_ID)
GROUP BY c.Category_ID
答案 1 :(得分:0)
感谢@ChrisPatrick的回答,这是我使用的代码:
SELECT
SUM(t.Points) AS Total_Points,
c.Title,
SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT t.reason SEPARATOR '|'), '|', 5) AS Sample_Reasons
FROM
transactions t JOIN categories c ON (t.Category_ID=c.Category_ID)
GROUP BY c.Category_ID
ORDER BY c.Title ASC