Everything I've seen so far is about removing duplicate entries in a database automatically. I want to stress at the beginning of this that there is no duplicate data in the database. I'll also start with the fact that I'm very much still learning about RDBMS design, normalization, relationships, and, most of all, SQL!
I have a table of clients, with a clientid (PK) and a client_name. I have a table of roles, with a roleid (PK) and a role_name. Any client can have multiple roles associated with it. So I created a client_role_link table, with clientid and roleid as the two fields. Then I run this:
SELECT client.client_name, role.role_name FROM client
LEFT JOIN client_role_link ON client_role_link.clientid=client.clientid
LEFT JOIN role ON client_role_link.roleid=role.roleid
WHERE (role.roleid='1' OR role.roleid='2')
So let's say I have a client that has two roles associated with it (roles '1' and '2'). This query returns two rows, one for each role. When I get these results back I'm using a while
loop to cycle through the results and output them into a <select>
list. It's then causing two <option>
's with the same client listed.
I understand why my query is returning two rows, it makes sense. So here comes the two fold question:
- Is there a better database/table design that I should be using, or a more optimized query?
- Or is this something I should handle in the PHP? If so, is there a more elegant solution that adding all the results into an array, then looping back through the array and removing duplicates?
Thoughts?