它的内容并不能完全回答问题,但是会建议您如何正确规范化表以简化问题。
这是一种
Many-to-Many关系。
Employees- ID (Primary Key)- Name- TypeTask- ID (Primary Key)- NameWork- EmployeeID (Foreign Key)- TaskID (Foreign Key)
员工表
id name type 1 john 2 2 peter 1 3 leah 2 4 frank 1 5 tang 3
任务表
id name 1 task1 2 task2 3 task3 4 task4
工作表
TaskID EmployeeID1113222431323344
询问,
SELECt t.ID, t.Name, STUFF( (SELECT ',' + b.Name FROM Work a INNER JOIN Employee b ON a.EmployeeID = b.ID WHERe a.TaskID = t.ID FOR XML PATH ('')) , 1, 1, '') AS NamesListFROM Task t-- WHERe ..... -- add additional conditions...GROUP BY t.ID, t.Name
- SQLFiddle演示
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)