mingqi
05-07-2011, 09:53 PM
Hi! I was wondering if the following would be possible:
I have a table called professors which has a many-to-many relationship with a table called courses, since a professor can teach more than one course. When I try to do a mysql query for a professor and the courses, I come up with the something like this:
Professor A GEO432
Professor A BIO101
Professor A PHY302
Professor B GEO432
Professor B SCI222
Professor B ECO154
When I want something like this:
Professor A GEO432 BIO101 PHY302
Professor B GEO432 SCI222 ECO 154
I want everything under Professor A to be in one row.
I tried the GROUP-CONCAT method, but it doesn't work very well with the application I am developing - part of which includes a search of the database. Depending on the field search, I either get all NULLs or all subjects possible in the table get concat-ed..seems like it is intent to have everything in one row when I just want each single professor to have his own row instead of multiple rows....i.e.
Searching for GEO 432
I would get
Professor A GEO 432, GEO 432 [<---instead of going to the next row to say Professor B teaches GEO 432 also, I get the subject repeated in the same row]
I digress...so anyways, I want to find another way. The case I am providing is just an simplified example of what I am trying to do. Hopefully that is enough information. Thank you!
I have a table called professors which has a many-to-many relationship with a table called courses, since a professor can teach more than one course. When I try to do a mysql query for a professor and the courses, I come up with the something like this:
Professor A GEO432
Professor A BIO101
Professor A PHY302
Professor B GEO432
Professor B SCI222
Professor B ECO154
When I want something like this:
Professor A GEO432 BIO101 PHY302
Professor B GEO432 SCI222 ECO 154
I want everything under Professor A to be in one row.
I tried the GROUP-CONCAT method, but it doesn't work very well with the application I am developing - part of which includes a search of the database. Depending on the field search, I either get all NULLs or all subjects possible in the table get concat-ed..seems like it is intent to have everything in one row when I just want each single professor to have his own row instead of multiple rows....i.e.
Searching for GEO 432
I would get
Professor A GEO 432, GEO 432 [<---instead of going to the next row to say Professor B teaches GEO 432 also, I get the subject repeated in the same row]
I digress...so anyways, I want to find another way. The case I am providing is just an simplified example of what I am trying to do. Hopefully that is enough information. Thank you!