I am trying to come up with a query where a 3rd table will join another two. The problem is there are different versions of this first table and I need the query to look at a column in table 1 and then choose the correct table to join but the resulting query must return all rows in table 1 just join an extra table 3 to specific rows which meet the WHERe clause. Is this even possible?
Example:
Table 1 = User
Table 2 = Banners
Table 3 = UserSettings OR AdminSettings OR Nothing
Query would say…
WHERE User.rank='admin' LEFT JOIN AdminSettings
WHERE User.rank='user' LEFT JOIN UserSettings
My current query is this:
$query = "SELECT * FROM Users as U WHERE U.user_id = :id ";
$query .= "LEFT JOIN Banners as B ON U.banner_id = B.banner_id ";
$query .= "LEFT JOIN AdminSettings ON U.server_id = AdminSettings.user_id WHERE U.rank='Admin' ";
$query .= "LIMIT 1";
Problem is that it only shows rows that have User.rank=’admin’ and does not show the rows where User.rank=’user’ rest.
Try using a IF clause in the fields list, like this:
Something like that should do the trick.