在某些条件下,在SQL选择语句中重复相同的行

时间:2011-11-11 13:28:30

标签: asp.net sql sql-server sql-server-2008

我有一个数据库表有四列名为"Courses(CourseID,CourseName,CreditHours,LabSession)",我想编写SQL语句来检索Course表中的所有数据,此外,如果LabSession列中的SQL必须重复行(记录) row具有值"Y",否则将按原样检索行而不重复。

BTW,我正在使用Microsoft SQL Server数据库

2 个答案:

答案 0 :(得分:2)

select CourseID,CourseName,CreditHours,LabSession from Courses
union ALL
select CourseID,CourseName,CreditHours,LabSession from Courses where LabSession='Y'
order by CourseID

答案 1 :(得分:2)

SELECT CourseID, CourseName, CreditHours, LabSession
FROM Courses
UNION ALL
SELECT CourseID, CourseName, CreditHours, LabSession
FROM Courses
WHERE LabSession = 'Y'
ORDER BY CourseId