简体   繁体   English

MySQL联接多表错误

[英]MySQL join multiple tables error

I struggle to use join on multiple tables. 我努力在多个表上使用联接。 When I try to do this: 当我尝试这样做时:

SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`, `type`
 LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID` 

I get this: 我得到这个:

Unknown column 'absences.employee_FK' in 'on clause'

'absences.employee_FK' exists in my DB. 我的数据库中存在“ absences.employee_FK”。

I want to display the user data and the type of the absence. 我想显示用户数据和缺席的类型。 How can I do that? 我怎样才能做到这一点? I dont understand joins too well yet. 我还不了解加入的技巧。

在此处输入图片说明

Looks like your just trying to join two tables, because you don't have a join condition for the type table in your query: 看起来您只是试图连接两个表,因为您的查询中没有type表的连接条件:

SELECT *
FROM absences
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID

If you want to join to the type table too: 如果您也想加入type表:

SELECT *
FROM absences
LEFT JOIN type ON absences.type_FK = type.type_ID
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID

You have to select all the tables for using the JOIN condition. 您必须选择所有表以使用JOIN条件。

The example goes like this: 该示例如下所示:

SELECT `employee.*`, `absences.*`, `type.*`

FROM `employee`

JOIN `absences`

ON `employee`.`employee_ID` = `absences`.`employee_FK`

JOIN `type` 

ON `absences`.`type_FK` = `type`.`type_ID`

JOIN `on_off`

ON `on_off`.`on_off_ID` = `employee`.`on_off_FK`;

You can modify the query as per your requirement. 您可以根据需要修改查询。

You have to use join for all tables: 您必须对所有表使用联接:

SELECT `absences`.*, `employee`.*, `type`.*
FROM `absences`
 JOIN `type` on `absences`.`type_fk` = `type`.`type_ID`
 LEFT JOIN `login`.`employee` ON `absences`.`employee_FK` = `employee`.`employee_ID` 

You can work on the script below. 您可以使用以下脚本。 Add Where clause at the end if necessary. 如有必要,在末尾添加Where子句。 Not tested... 未测试...

SELECT * from absences a
inner join type t on (t.typeID = a.type_FK)
inner join employee e on (e.employee_ID = a.employee_FK)

This might be what you are looking for 这可能是您要寻找的

select * from `absences` a
    left outer join `employee` e on e.`employee_ID` on a.`employee_FK`
    left outer join `type` t on t.`type_ID`=a.`type_FK`
    left outer join `on_off` o on o.`on_off_ID`=e.`on_off_FK`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM