What I have
I have multiple Tables like this:
- tbl_hw_inventar (hw_id, hostname, hw_typ_idfs, hw_created_user_idfs, etc...)
- tbl_hw_typ (hw_typ_id, hw_typ_title, etc...)
- tbl_user (user_id,username, etc...)
- tbl_hw_edited (hw_edited_id, hw_edited_client_idfs, hw_edited_date, hw_edited_user_idfs)
What I need
I want output a table with the following information:
hw_id | Hostname | created | last edited
12315 | client-01 | 2015-05-06 15:31:06 (username) | 2015-07-02 09:46:17 (username) |
The problem
As you can see, I can get information like the hw_typ with a foreign key and a inner join to the "tbl_hw_typ". The same for information with "hw_created_user_idfs" and a inner join to get the username for the userid.
But how can I get the last edited, datetime and the username?
In my table "tbl_hw_edited" i have entries like this:
row id | hw_id | datetime | user_id
The code
My SQL query looks like this so far:
SELECT `tbl_hw_inventar`.*, `tbl_hw_typ`.`hw_typ_title`, `tbl_user`.`username`, `tbl_hw_edited`.`hw_edited_id`
FROM `tbl_hw_inventar`
INNER JOIN `tbl_hw_typ`
ON `tbl_hw_inventar`.`hw_typ_idfs` = `tbl_hw_typ`.`hw_typ_id`
INNER JOIN `tbl_user`
on `tbl_hw_inventar`.`hw_create_user_idfs` = `tbl_user`.`id`
JOIN (
SELECT MAX(`tbl_hw_edited`.`hw_edited_id`), `tbl_hw_edited`.`hw_edited_client_idfs`
FROM `tbl_hw_edited`
) `tbl_hw_edited` ON `tbl_hw_inventar`.`hw_id` = `tbl_hw_edited`.`hw_edited_client_idfs`
ORDER BY `tbl_hw_inventar`.`hw_id` ASC
So how can I export the information? It looks like I have to make a subquery in my query. But I failed with every try.
Thanks for you help
EDIT
As proposed I'm providing more information (table data) for each table:
-tbl_hw_inventar-
| hw_id | hw_hostname | hw_create_date | hw_create_user_idfs |
| 1 | client-01 | 2015-03-06 11:57:42 | 1 |
| 2 | client-02 | 2015-09-21 21:17:00 | 3 |
-tbl_hw_edited-
| hw_edited_id | hw_edited_client_idfs | hw_edited_date | hw_edited_user_idfs |
| 1 | 1 | 2015-09-24 17:30:22 | 1 |
| 2 | 2 | 2015-09-24 16:33:22 | 2 |
| 3 | 1 | 2015-09-24 23:30:22 | 2 |
| 4 | 2 | 2015-09-24 20:30:22 | 3 |
-tbl_user-
| id | username |
| 1 | ismaelw |
| 2 | skalb |
| 3 | yrumpel |
So as a final result I need an output like this:
| hw_id | hostname | created | edited |
| 1 | client-01 | 2015-03-06 11:57:42 (ismaelw) | 2015-09-24 23:30:22 (skalb) |