有没有办法查询数据库以列出列表中的培训师计划,或者这是我们应该在Visual Basic中做的事情
我被告知要使用sql和/或visual basic执行此操作,我已尝试使用一些wierd for foreach或while循环,但不是很远。从项目中我们应该使用纯c#或sql了。
我目前创建了一个SQL查询:
SELECT
Trainers.ID, Trainers.FirstName, Trainers.LastName, Trainers.Phone,
Trainers.Hours, TrainerPlan.PlanID
FROM
FitnessPlans
INNER JOIN
(Trainers INNER JOIN TrainerPlan ON Trainers.ID = TrainerPlan.TrainerID) ON FitnessPlans.ID = TrainerPlan.PlanID;
其中包含导致此问题的培训师列表:
ID FirstName LastName Phone Hours PlanID
--------------------------------------------------------------
1 Calvin Hodges 765-495-1234 MWF: 8 am - 4 pm 1
1 Calvin Hodges 765-495-1234 MWF: 8 am - 4 pm 2
1 Calvin Hodges 765-495-1234 MWF: 8 am - 4 pm 3
1 Calvin Hodges 765-495-1234 MWF: 8 am - 4 pm 4
1 Calvin Hodges 765-495-1234 MWF: 8 am - 4 pm 5
1 Calvin Hodges 765-495-1234 MWF: 8 am - 4 pm 6
1 Calvin Hodges 765-495-1234 MWF: 8 am - 4 pm 7
1 Calvin Hodges 765-495-1234 MWF: 8 am - 4 pm 8
2 Brittany Saxony 765-495-9876 TH: 8 am - 4 pm 3
2 Brittany Saxony 765-495-9876 TH: 8 am - 4 pm 5
2 Brittany Saxony 765-495-9876 TH: 8 am - 4 pm 6
2 Brittany Saxony 765-495-9876 TH: 8 am - 4 pm 7
2 Brittany Saxony 765-495-9876 TH: 8 am - 4 pm 8
3 Jacob Golden 765-495-1111 MTWHF: 4 pm - 12 am 1
3 Jacob Golden 765-495-1111 MTWHF: 4 pm - 12 am 2
3 Jacob Golden 765-495-1111 MTWHF: 4 pm - 12 am 3
3 Jacob Golden 765-495-1111 MTWHF: 4 pm - 12 am 4
3 Jacob Golden 765-495-1111 MTWHF: 4 pm - 12 am 5
3 Jacob Golden 765-495-1111 MTWHF: 4 pm - 12 am 6
4 Lisa Harris 765-494-0987 MTWHF: 4 pm - 12 am 3
4 Lisa Harris 765-494-0987 MTWHF: 4 pm - 12 am 5
4 Lisa Harris 765-494-0987 MTWHF: 4 pm - 12 am 6
4 Lisa Harris 765-494-0987 MTWHF: 4 pm - 12 am 7
4 Lisa Harris 765-494-0987 MTWHF: 4 pm - 12 am 8
它应该是这样的:
1 Calvin Hodges 765-495-1234 MWF: 8 am - 4 pm 1,2,3,4,5,6,7,8
2 Brittany Saxony 765-495-9876 TH: 8 am - 4 pm 3,5,6,7,8
3 Jacob Golden 765-495-1111 MTWHF: 4 pm - 12 am 1,2,3,4,5,6
4 Lisa Harris 765-494-0987 MTWHF: 4 pm - 12 am 3,5,6,7,8
有什么想法吗?
答案 0 :(得分:1)
您需要透视表link
给你
答案 1 :(得分:0)
尽管Anand已经提出了一个通常与聚合函数(sum,min,max,avg等)相关联的数据透视表,但我不相信它可以用于做MySQL允许作为" Group_Concat ()"功能..
但是,我发现了一个适用于您的解决方案from this link ...已修改以满足您的需求。
SELECT
T.ID,
T.FirstName,
T.LastName,
T.Phone,
T.Hours,
PreQueryPlans.AllPlans
from
( select
TP.TrainerID,
stuff( ( select cast(',' as varchar(max)) + str(TP2.PlanID,3)
from TrainerPlan TP2
WHERE TP.TrainerID = TP2.TrainerID
order by TP2.PlanID
for xml path('') ), 1, 1, '') AS AllPlans
from
TrainerPlan TP
group by
TP.TrainerID ) PreQryPlans
JOIN Trainers T
on PreQryPlans.TrainerID = T.ID