Hare Posted June 25, 2017 Share Posted June 25, 2017 (edited) Hello! I have an SQL question about joining 3 tables for my game? I have rabbits and guinnea pigs in my SIM. When you look in your shed, you see all your cages. The cages can either be empty, contain a rabbit, or contain a guinnea pig. I got this to work (shown in screenshot), but it doesn't allow you to ORDER BY pig and rabbit data. I can get ORDER BY working if I join the Cages_Table with either Rabbit_Table or Pig_Table table, but not both. The rabbit and pig tables don't have a common ID with each other, they only link to the Cage_Table. The Cage_Table has a column for rabbit ID and a column for pig ID. 0 means none. If both are 0, the cage is empty (one of them will always be 0). Is possible to join the Cages_Table to both Rabbit_Table AND Pig_Table or do I need to go about this another way? Any help would be much appreciated. I've been messing with this thing all day without much luck and am pretty baffled. Not sure if what I'm trying to do is even possible. This is the code that works but only joins rabbits to cages: SELECT CAGE_TABLE.* FROM CAGE_TABLE LEFT JOIN RABBIT_TABLE ON (CAGE_TABLE.rabbitid=RABBIT_TABLE.rabbitid) WHERE CAGE_TABLE.shed='$shedid' ORDER BY $sort1 ASC, $sort2 ASC And this is one of the codes I tried that doesn't work (I've been trying a lot of other stuff though): SELECT CAGE_TABLE.* FROM CAGE_TABLE LEFT JOIN RABBIT_TABLE ON (CAGE_TABLE.rabbitid=RABBIT_TABLE.rabbitid) LEFT JOIN PIG_TABLE ON (CAGE_TABLE.pigid=PIG_TABLE.pigid) WHERE CAGE_TABLE.shed='$shedid' ORDER BY $sort1 ASC, $sort2 ASC Screenshot of the working code in action. If only I could sort the pigs and rabbits by breed and other data, and get the empty cages to show up last, it would be perfect. Please excuse the -1 years and months old, that'll be fixed later. Edited June 25, 2017 by Hare Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.