Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 591505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:38:29+00:00 2026-05-13T15:38:29+00:00

Everything I’ve seen so far is about removing duplicate entries in a database automatically.

  • 0

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:

  1. Is there a better database/table design that I should be using, or a more optimized query?
  2. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:38:30+00:00Added an answer on May 13, 2026 at 3:38 pm

    If you want to show both roles, then your query is OK.

    MySQL does not support array datatypes, so you should fill an associative array on the PHP side using the resultset with the duplicate client names.

    If you just need to show clients having either of the roles, use this query:

    SELECT  c.*
    FROM    client c
    WHERE   c.clientid IN
            (
            SELECT  roleid
            FROM    client_role_link crl
            WHERE   crl.roleid IN (1, 2)
            )
    

    This will return one record per client but won’t show any roles.

    The third way would implode the role names on the server side:

    SELECT  c.*, GROUP_CONCAT(role_name SEPARATOR ';') AS roles
    FROM    client c
    LEFT JOIN
            client_role_link crl
    ON      crl.clientid = c.clientid
            AND crl.roleid IN (1, 2)
    LEFT JOIN
            role r
    ON      r.roleid = crl.roleid
    GROUP BY
            c.id
    

    and explode them on PHP side, but make sure role names won’t mix with the separator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Everything I've seen and experienced so far suggests that you must use Windows Authentication
Everything I'm finding via google is garbage... Note that I want the answer in
Everything that I read about sockets in .NET says that the asynchronous pattern gives
Everything I know about .Net programming tells me that the behavior I see here
Everything is in the title, I want to be able to delete account that
Everything I am reading about rails 3 and AJAX says that we should have
Everything I read about cookies says that setting the expiry time of a cookie
Everything about this call works, except the picture doesn't display. I found one other
Everything about this script works just fine but except for the e.PreventDefault(); I've tried
Everything is in the question : I have a Php script that is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.