1

我有一个为我正在制作的网站创建的数据库,其中包含员工列表。这将是一个网格状列表,包含每个员工的照片、姓名、电话号码等。

这是我的第一个数据库设计,我为此感到非常自豪……但我是新手,所以我确信它在某些方面效率低下。

我无法找出为网站查询它以制作元素的最佳方式。

1)我应该使用虚拟表吗?或者我应该只使用一个查询并为每个员工循环它?

2) 一些员工将拥有多个司法管辖区。现在我有:

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)

哪个会返回:

+-----------+----------+--------------+-----------------+---------+--------------+
| 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       |
+-----------+----------+--------------+-----------------+---------+--------------+

由于员工拥有额外的管辖权,有什么方法可以创建删除重复行的查询?就像是?

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

这是我的mysql架构:

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);
4

1 回答 1

3

您可以按员工对结果进行分组,并使用 MySQL 的GROUP_CONCAT()功能来汇总员工的管辖范围:

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

但是,这会将管辖区聚合成一个分隔字符串(这使得很难区分多个管辖区和包含分隔符的单个管辖区:人们可以选择一个不太可能在管辖区名称中使用的分隔符,但它仍然会遇到诸如作为字符串长度的限制,并且在我看来,从根本上来说是懒惰的)。

更好的方法可能是将这种聚合留给更高级别的应用程序代码。例如,使用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

}

当然,这样做的缺点是产生了一个更大的结果集,这些结果集必须由 RDBMS 生成并传输到您的更高级别代码/由您的更高级别代码处理。

于 2012-12-19T21:35:42.993 回答