Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
If I have two tables, say subject_lesson and lesson
Now, lessonIDs in subject_lesson table stores lessonID seperated by comma.. as shown below..

lesson table
lessonIDlessoname
1Some lesson name
2another lesson name
3another lesson

and so on...

and in subject_lesson table
I have stored values from a form as follows:

subject_lesson
studylessonIDstudyIDlessonIDs
131,3,


studylessonID (bigint) primary_key auto_increment 1
studyID (bigint) foreign_key --------->(A third table named subject where studyID is primary)
lessonIDs (varchar(100))

lessonID (bigint) primary_key auto_increment 1
lessoname (varchar(100)
where '3' is a studyID corresponding to a subject name from subject table..

In short, the gridview contains 3 columns as [Course] [Subject] [Lessons] .. Now these [course] and [subject] are simple fetch from subject_lesson table using the studyID and INNER JOIN to subject table. but the third column has values in database as 1,3,(lessonIDs in subject_lesson) .... instead of this 1,3, I want to display the lesson names as "Some lesson name, another lesson,"....using the lesson table.. I'm not sure if I made it clear! :(
Posted
Updated 16-Sep-13 10:41am
v2

1 solution

Test it:
SQL
DECLARE @lessons TABLE(LessonID INT IDENTITY(1,1), LessonName VARCHAR(30))

INSERT INTO @lessons (LessonName)
SELECT 'lesson A'
UNION ALL SELECT 'lesson B'
UNION ALL SELECT 'lesson C'
UNION ALL SELECT 'lesson D'
--UNION ALL SELECT 'lesson E'


DECLARE @subject_lesson TABLE (slid INT IDENTITY(1,1), lids VARCHAR(30))

INSERT INTO @subject_lesson (lids)
SELECT '1,2,3'
UNION ALL SELECT '2,3,4'
UNION ALL SELECT '1,3,5'

;WITH Lessons AS
(
	SELECT slid, CONVERT(INT, LEFT(lids, CHARINDEX(',', lids)-1)) AS lid, RIGHT(lids, LEN(lids) - CHARINDEX(',', lids)) AS Remainder
	FROM @subject_lesson
	WHERE CHARINDEX(',', lids)>0
	UNION ALL 
	SELECT slid, CONVERT(INT, LEFT(Remainder, CHARINDEX(',', Remainder)-1)) AS lid, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(',', Remainder)) AS Remainder
	FROM Lessons
	WHERE CHARINDEX(',', Remainder)>0
	UNION ALL
	SELECT slid, CONVERT(INT, Remainder) AS Remainder, NULL AS Remainder
	FROM Lessons
	WHERE CHARINDEX(',', Remainder)=0
)
SELECT L.slid, L.lid, M.LessonName
FROM Lessons AS L LEFT JOIN @lessons AS M ON L.lid = M.LessonID
ORDER BY L.slid, L.lid


Result:
slid   id  LessonName
1      1   lesson A
1      2   lesson B
1      3   lesson C
2      2   lesson B
2      3   lesson C
2      4   lesson D
3      1   lesson A
3      3   lesson C
3      5   NULL
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Sep-13 16:17pm    
5ed. Considerable effort.
—SA
Maciej Los 16-Sep-13 16:19pm    
Thank you, Sergey ;)
Rahul Vohra 16-Sep-13 16:35pm    
Really appreciate your quick response Maciej Los, but as I mentioned I want to do the same in c# and display the result in a data grid view.
I have no knowledge of what you have posted. I guess its T-SQL, but I wanted in c#.
So, in short I want to fetch lessonIDs from database -> use a loop to fill a datatable with lesson names corresponding to fetched lessonIDs, and then display these lesson names, in one of the columns of the gridview. (I said one of the columns as the gridview contains other columns as [Course] [Subject] [Lessons] .. Now these [course] and [subject] are simple fetch from subject_lesson table (as mentioned in my question) using the studyID and INNER JOIN to subject table. but the third column has values in database as 1,3, .... instead of this 1,3, I want to display the lesson names.... I'm not sure if I made it clear! :(
Rahul Vohra 16-Sep-13 16:42pm    
And I forgot to mention, I don't want to create another table, as I cannot change the database structure.. Cannot add or remove tables.. !!So, I need to do this in the code behind!
Thanks7872 17-Sep-13 0:44am    
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900