Click here to Skip to main content
15,894,252 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a database with a many to many relationship. Tables: Course, textBooks, BookCourse.

One Course can have many books, many courses can have the same book.

I made a nested gridview showing the Course, and then upon expansion of the course it shows every book associated with it. But in my current select query, It displays duplicate rows in the Course table for each book. When a book is entered, the CourseID to add another book to that Course, but right now it is adding a duplicate row to my parent Course Table instead of just adding it to the textbook table and displaying it under the Course with that CourseID

https://gyazo.com/d576a7835b1727277ceedf7787d35787

The above link shows how the Course table grid view is showing duplicates. if expanded on, there are multiple books under duplicate rows.

What I have tried:

string query = "select DISTINCT * from Course inner join textBooks on textBooks.CourseID = Course.CourseID";
Posted
Updated 22-Aug-16 10:20am
v2
Comments
Anisuzzaman Sumon 23-Aug-16 1:23am    
Would not it be better to show the definitions of those tables to get accurate answer?

Use DISTINCT, but don't use *. Because you are using * then all fields from all tables are being returned and therefore DISTINCT probably doesn't help much. Use DISTINCT but also just select the fields you need, not everything. Simple.
 
Share this answer
 
Comments
Member 12697758 22-Aug-16 17:18pm    
I have to select all otherwise I get an error saying something doesnt exist in the current content from the table where I didnt select *.
ZurdoDev 22-Aug-16 19:09pm    
Then change that, or else you'll get duplicates.
Another solution would be to try a different approach to your table design. Instead of a relating a book to a course in the book table, it is a better design to have a table of courses, a table of books, and a relationship table to define which books go to which courses. Your sql statements would have to join through the relationship table which makes the initial set up and coding a bit more difficult but makes maintaining it much more simple.
 
Share this answer
 

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