此问题是以下问题的继续: How do I convert this Access Query to mySQL query?
我想在我的代码中将以上内容表示如下:
Access SQL FROM语句如下
FROM qryvw_employees
INNER JOIN ((Tbl_Clients INNER JOIN (Tbl_Assignments
INNER JOIN Tbl_Tasks ON Tbl_Assignments.Assignment_ID = Tbl_Tasks.Assignment_ID) ON Tbl_Clients.PAN = Tbl_Assignments.PAN)
INNER JOIN qryvw_subtasks ON Tbl_Tasks.TaskID = qryvw_subtasks.TaskID) ON qryvw_employees.ID = Tbl_Tasks.Assigned_To
基于此查询中的回复,我正在尝试以下操作:
FROM
qryvw_employees
INNER JOIN (tbl_clients
INNER JOIN tbl_assignments ON tbl_clients.`PAN` = tbl_assignments.`PAN`
INNER JOIN tbl_tasks ON tbl_assignments.`Assignment_ID` = tbl_tasks.`Assignment_ID`
INNER JOIN qryvw_subtasks ON tbl_tasks.`TaskID` = qryvw_subtasks.`TaskID`) ON qryvw_employees.`ID` = tbl_tasks.`Assigned_To`
我试图实现的上述关系图如下: enter image description here
plz协助,请勿在信誉低下删除。这是真正正确的疑问,因为两个表之间存在一对多的关系,而上面的问题并未对此进行介绍。
我的问题是我如何在SQL语句中表示两个一对多的关系(如图所示)。这是如何将此访问查询转换为mySQL查询的延续。上面的链接。
答案 0 :(得分:0)
做一个事实,就是您只是在使用INNER查询,而不应该使用()和嵌套子查询..
select your_col1, your_col2, ..., your_colN
FROM qryvw_employees
INNER JOIN Tbl_Assignments
INNER JOIN Tbl_Tasks ON Tbl_Assignments.Assignment_ID = Tbl_Tasks.Assignment_ID
INNER JOIN Tbl_Clients ON Tbl_Clients.PAN = Tbl_Assignments.PAN
INNER JOIN qryvw_subtasks ON Tbl_Tasks.TaskID = qryvw_subtasks.TaskID
INNER JOIN ON qryvw_employees.ID = Tbl_Tasks.Assigned_To
无论如何在mysql中,每个嵌套的子查询都应使用适当的别名来命名,例如: 选择(从my_table中选择...。)t1_alias
答案 1 :(得分:0)
我认为您的from子句将如下所示
FROM qryvw_employees INNER JOIN
((Tbl_Clients INNER JOIN
(Tbl_Assignments INNER JOIN Tbl_Tasks
ON Tbl_Assignments.Assignment_ID = Tbl_Tasks.Assignment_ID)
ON Tbl_Clients.PAN = Tbl_Assignments.PAN)
INNER JOIN qryvw_subtasks ON Tbl_Tasks.TaskID = qryvw_subtasks.TaskID)
ON qryvw_employees.ID = Tbl_Tasks.Assigned_To
答案 2 :(得分:0)
FROM
子句产生的子任务列表乘以与该子任务分配相关的所有客户端。
您可以从任何一个表开始,这取决于您。让我们保留此雇员表。然后加入下一张桌子。与员工相关的表是任务表。接受并加入到员工ID中。下一个要联接的表可以是子任务表或工作分配表。选择任何一个。依此类推,请始终将下一个表添加到现有表中。
from qryvw_employees e
join tbl_tasks t on t.assigned_to = e.id
join qryvw_subtasks st on st.taskid = t.taskid
join tbl_assignments a on a.assignment_id = t.assignment_id
join tbl_clients c on c.pan = a.pan
如果您未从SELECT
子句的所有表中选择列,或者查询中存在某些聚合({{1},SUM
,...,{{1 }},也许是COUNT
?,那么您可能希望将派生表(子查询)联接起来或将表移到GROUP BY
子句中以提高可读性和/或性能。没看清您的实际选择,这就是我所能提供的所有建议。
答案 3 :(得分:0)
当语句由Access iteself生成时,查询在MySQL中有效。无需更改。这是下面的答案:
FROM
qryvw_employees
INNER JOIN (tbl_clients
INNER JOIN tbl_assignments ON tbl_clients.`PAN` = tbl_assignments.`PAN`
INNER JOIN tbl_tasks ON tbl_assignments.`Assignment_ID` = tbl_tasks.`Assignment_ID`
INNER JOIN qryvw_subtasks ON tbl_tasks.`TaskID` = qryvw_subtasks.`TaskID`) ON qryvw_employees.`ID` = tbl_tasks.`Assigned_To`