I am trying to join 3 tables together but I keep getting ‘unknown table ‘calls’ in field list. I know the table ‘calls’ exists
This works…
$sql = "SELECT * FROM calls WHERE id = '$diary_id'";
but this doesn’t…
$sql = "SELECT * FROM (SELECT calls.id AS calls_id, calls.assigned_user_id AS assigned_user) calls
RIGHT JOIN accounts on accounts.parent_id = accounts.id
LEFT JOIN users on assigned_user = user.id
WHERE calls_id = '$diary_id'";
I am trying to use aliases because the tables I am trying to join have the same field names (an inherited database I have to use).
Any help will be greatly appreciated
Your subquery:
is selecting
calls.idandcalls.assigned_userbut there is noFROMclause from which thecallstable/alias is defined. AddFROM callsto the subquery (although why use a subquery here at all?).You’re also referring to
user.idin your join criterion, but the table is calledusers.And your join criterion for the accounts table,
accounts.parent_id = accounts.id, looks very suspect: it appears that you’re trying to traverse hierarchical data that has been stored using the adjacency list model, but this join won’t accomplish that; indeed, MySQL doesn’t support recursive functions—so it is not well suited to this model at all. You might consider restructuring your data to use either nested sets or closure tables. See this answer for more information.Instead: