2

Is it posibble to select 3 tables at a time in 1 database?

Table 1: employee
              --> employee_id
              --> first_name
              --> last_name
              --> middle_name
              --> birthdate
              --> address
              --> gender
              --> image
              --> salary

Table 2: logs
              --> log_id
              --> full_name 
              --> employee_id
              --> date
              --> time
              --> status

Table 2: logout
              --> log_id
              --> full_name 
              --> employee_id
              --> date
              --> time
              --> status

I wanted to get the value of employee table where $id of selected. Then the $id also get the value of log.time, log.date, logout.time, and logout.date.

I already try using UNION but nothing happens.

1
  • What do you mean by "nothing happens"? Could you post the exact query that you're having problems with? Commented Jun 2, 2010 at 6:05

3 Answers 3

5

Something like:

SELECT * FROM `employee` as `e` ( LEFT JOIN `logs` ON `e`.`employee_id` = `logs`.`employee_id`) LEFT JOIN `logout` ON `e`.`employee_id` = `logout`.`employee_id` WHERE `e`.`employee_id` = $id

You want to join the tables together using their related column, the employee id. This sql statement first joins employee to logs, then that result is joined to logout.

Sign up to request clarification or add additional context in comments.

Comments

3

Up to you to put it in your app, but the query looks like this. Dan's query will not return exactly what you said you're looking for.

SELECT e.*, l.time, l.date, lo.time, lo.date 
  FROM employee e
  LEFT JOIN logs l 
    ON l.employee_id = e.employee_id
  LEFT JOIN logout lo
    ON lo.employee_id = e.employee_id
  WHERE e.employee_id = {your id here}

2 Comments

But how can I get the value of l.time? If i use $row['time']; only the lo.time is showed.
In the first line, change "lo.time" to "lo.time AS logout_time". Now you should be able to access $row['logout_time']
1

Jordan,

To expand on the query Entendu gave (I can't reply to that), you can use aliases:

SELECT e.*, l.time AS l_time, l.date AS l_date, lo.time AS lo_time, lo.date AS lo_date

FROM employee e LEFT JOIN logs l ON l.employee_id = e.employee_id LEFT JOIN logout lo ON lo.employee_id = e.employee_id WHERE e.employee_id = {your id here}

This way you can call $row['l_time'] to get the value of l.time

Comments

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.