boxhead — 2011-01-18T06:44:48-05:00 — #1
I have a table of people and a table of roles. A person can have several roles. I have a query which joins the two. How do I only return one of the joins per person - I don't care which join it returns. I needs all columns from both tables.
This is in MSSQL
eruna — 2011-01-18T07:52:51-05:00 — #2
It sounds like you need an intermediary table matching the primary key for roles and the primary key for people.
boxhead — 2011-01-18T08:02:09-05:00 — #3
Explain further! The Key of the person is linked to a foreign key in the roles.
timigoe — 2011-01-18T08:31:56-05:00 — #4
If you are linking from one person to many rules, but don't care which is returned then you can group on the user's id (the primary key of the first table) and that'll return a single line for users then.
I'm not sure about MS SQL but I know mySQL has a 'group_concat' function which would allow you to make, for example, a comma separated list of roles.
guido2004 — 2011-01-18T08:40:12-05:00 — #5
So 1 person can have many roles, but 1 role can be linked to only 1 person? There are no roles that can be of many persons?
Of course, it's hard to tell without knowing what a 'role' is, and what data the 'roles' table contains.
r937 — 2011-01-18T09:12:42-05:00 — #6
please script out all the tables (there should be three of them) so that we can see the CREATE TABLE statements
oddz — 2011-01-19T13:37:58-05:00 — #7
This is formatting that would be done in the application language after the result has been returned from the database with all the duplicates in raw form.