I have a table with double foreign key reference to gebruikersid and now I’m kind of in a mess. First of all a “gebruiker” is a “user” in dutch, to avoid miscommunications.
The query I’m using now is:
$uploadeditems = "
SELECT *
FROM file f
INNER JOIN gebruikers g on f.empid = g.gebruikersid
WHERE g.gebruikersid = ".$GET['employeeid']."
";
But what I actually would need to make is this something like this:
$uploadeditems = "
SELECT *
FROM file f
INNER JOIN gebruikers on f.empid = gebruikers.gebruikersid as 'gebruikers.employee'
INNER JOIN gebruikers on f.gebruikersid = gebruikers.gebruikersid as 'gebruikers.uploader'
WHERE gebruikers.employee= ".$GET['employeeid']."
";
Short about the code so you get my point:
Table FILE has gebruikersid, who is the uploader of the file and empid, the one on whose this file has to be linked. (both FK of gebruiker.gebruikersid).
So for instance I have in my database:
fileid filename filesize filepath custid empid gebruikersid filedescription
228 Test. 60 files/employees/113/ NULL 113 70
So what I’m basically am trying is to have two references:
When I’m inner joining by f.empid I want to be able to return the username of the f.empid & gebruiker.gebruikersid reference, and with the same time I want to be able to return the username of the f.gebruikersid & gebruikers.gebruikersid reference, so what I could have return would be like:
File list of user X:
File A, uploaded by user Y.
File B, uploaded by user Z.
and so on…
Now I get:
File list of user X:
File A, uploaded by user X.
File B, uploaded by user X.
even when it’s all good in the database.
EDIT Since I had an employee table before (and now all employees are users) my code names are messed up, it might be a little harder sorry. need to figure this all out once I can move on.
If you join to the same table more than once, than you need to use an alias for at least one of the instances. Then you must use the alias to prefix all the fields you select from the tables.
For example: