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 9011225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:40:25+00:00 2026-06-16T02:40:25+00:00

I have a database that I made for a website I am making that

  • 0

I have a database that I made for a website I am making that will have a list of employees. It will be a grid like list with each employee pic, name, phone number, etc.

This is my first database design and I’m really proud of it… but I’m a newbie so I am sure it is inefficient in some ways.

I am having trouble figuring out the best way to query it for the website to make the elements.

1) Should I use a virtual table? Or should I just use one query and loop it for each employee?

2) Some employees will have more than one jurisdiction. Right now I have:

select employees.firstName, employees.lastName, employees.phone, employees.position,
    employees.picture, jurisdictions.jurisdiction
from employees
    inner join(jurisdictions cross join departments cross join user_jurisdictions)
    on (employees.deptID = departments.deptID
        AND user_jurisdictions.userID = employees.userID
        AND user_jurisdictions.jurID = jurisdictions.jurID)

Which would return:

+-----------+----------+--------------+-----------------+---------+--------------+
| firstName | lastName | phone        | position        | picture | jurisdiction |
+-----------+----------+--------------+-----------------+---------+--------------+
| John      | Smith  | 210-226-3232 | Senior Manager   |/pics/jpeg1|South America|
| John      | Smith  | 210-226-3232 | Senior Manager   |/pics/jpeg1|London       |
+-----------+----------+--------------+-----------------+---------+--------------+

How is there a way to create the query where it would remove the duplicates rows because of the additional jurisdiction the employee would have? something like?

+-----------+----------+--------------+---------+---------+--------------+------------+
| firstName | lastName | phone        | position| picture | jurisdiction |Jurisdiction|
+-----------+----------+--------------+---------+---------+--------------+------------+
| John      | Smith  | 210-226-3232 | Manager |/pics/jpeg1| South America|London      |
+-----------+----------+--------------+-----------------+---------+-------------------+

Here is my mysql schema:

employees
--------------------
userID (primary key)
firstName
lastName
phone
fax
position
picture
deptID (foreign key references departments(deptID)) 



departments
--------------------
deptID (primary key)
department

jurisdictions
-----------------
jurID (primary key)
jurisdiction


user_jurisdictions
--------------------------
userID(foreign key references employees(userID)) 
jurID(foreign key references jurisdictions(jurID))


triger: 
 after_employees_insert | INSERT | employees | begin
insert into user_jurisdictions(userID) values (new.userID);
  • 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-06-16T02:40:26+00:00Added an answer on June 16, 2026 at 2:40 am

    You can group your results by employee and use MySQL’s GROUP_CONCAT() function to aggregate the employee’s jurisdictions:

    SELECT   employees.firstName,
             employees.lastName,
             employees.phone,
             employees.position,
             employees.picture,
             GROUP_CONCAT(jurisdictions.jurisdiction)
    FROM     employees
        JOIN departments        USING (deptID)
        JOIN user_jurisdictions USING (userID)
        JOIN jurisdictions      USING ( jurID)
    GROUP BY employees.userID
    

    However, this aggregates jurisdictions into a delimited string (which makes it difficult to differentiate between multiple jurisdictions and a single jurisdiction containing the delimiter: one can choose a delimiter that is unlikely to be used in a jurisdiction name, but it’s still subject to problems such as limitations in string length and, in my view, is fundamentally lazy).

    A better way might be to leave such aggregation to a higher-level of your application code. For example, using PDO:

    $dbh = new PDO("mysql:dbname=$dbname", $username, $password);
    
    $qry = $dbh->query('
      SELECT   employees.userID
               employees.firstName,
               employees.lastName,
               employees.phone,
               employees.position,
               employees.picture,
               jurisdictions.jurisdiction
      FROM     employees
          JOIN departments        USING (deptID)
          JOIN user_jurisdictions USING (userID)
          JOIN jurisdictions      USING ( jurID)
      ORDER BY employees.userID
    ');
    
    $row = $qry->fetch();
    while ($row) {
      $current_user = $row['userID'];
    
      // handle $current_user initialisation
      do {
        // handle user $row
      } while ($row = $qry->fetch() and $row['userID'] == $current_user);
      // handle $current_user termination
    
    }
    

    Of course, this has the disadvantage of producing a much larger resultset that must be generated by the RDBMS and transferred to / processed by your higher-level code.

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

Sidebar

Related Questions

I made similar code with that but I also have database on one website
I have a pre-made SQLite database that I am downloading from the net via
I have a mySQL database that tracks our projects and drives our website's display
I have a Section in my website called Recent Updates. in that I will
I have a website (http://nottingham.subverb.net) that is mainly made up of messy procedural code
I have a website that is fairly database intensive, so I'm trying to cut
I have a database class that I made that uses PDO to connect to
i made this script that add in a mysql database information about who have
I have a website made to provide free web-based tools for making indie games.
i have DataBase function that calculate distance by coordinates CREATE OR REPLACE FUNCTION distance(lat1

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.