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);
You can group your results by employee and use MySQL’s
GROUP_CONCAT()function to aggregate the employee’s jurisdictions: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:
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.