1

有没有办法查询数据库以在列表中列出培训师计划,或者这是我们应该在 Visual Basic 中做的事情

我被告知要使用 sql 和/或 Visual Basic 执行此操作,我尝试使用一些奇怪的 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

有任何想法吗?

4

2 回答 2

1

你需要数据透视表,链接

为你

于 2013-11-09T12:11:47.400 回答
0

尽管 Anand 提出了一个通常与聚合函数(sum、min、max、avg 等)相关联的数据透视表,但我认为它不适用于 MySQL 允许的“Group_Concat()”功能。

但是,我确实从这个链接中找到了一个适合您的解决方案......不过已修改为适合您的需求。

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
于 2013-11-09T13:04:59.120 回答