1

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.

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.

enter image description here

1
  • What's follows left join show be a table name. So, is login.employee a table name? That means login is a database name and employee is a table in login. What's the output of show create table absences is like? Maybe you misspelled the name of the column. Commented Aug 14, 2015 at 8:51

5 Answers 5

2

Looks like your just trying to join two tables, because you don't have a join condition for the type table in your query:

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

If you want to join to the type table too:

SELECT *
FROM absences
LEFT JOIN type ON absences.type_FK = type.type_ID
LEFT JOIN employee ON absences.employee_FK = employee.employee_ID
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it worked I think I had a logical error. There was no need to join type too I guess.
1

You have to select all the tables for using the JOIN condition.

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.

1 Comment

@Jens.. Thanks for editing. I should have corrected that.
0

You can work on the script below. Add Where clause at the end if necessary. 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)

Comments

0

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`

Comments

0

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` 

1 Comment

Getting "Unknown column 'absences.type_FK' in 'on clause' again".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.