Ashley Sheridan;11018433 wrote:In the FROM and JOIN parts, I assign the full table names a shorter name. It just makes it easier than writing out the full table name, and reduces the chance of a typo.
I believe this is referred to as using a table alias. You can use the SQL reserved key word "AS" to assign a shorter, handier name to either selected values or the tables. Aliases of selected values are handy for distinguishing which value is from one table and which is from another. For example, if you have Table1 and Table2 that both have a name field, you can distinguish which one is which in your result by aliasing your selected values:
SELECT Table1.name as name1, Table2.name as name2
FROM Table1, Table2
WHERE Table1.id=Table2.id
Far more often, I find myself aliasing table names. You don't need the AS keyword for this -- just a space. In this example, I'm using table aliases (t1 and t2) to shorten my really long table names:
Select t1.name, t2.description FROM
my_really_long_table_name_for_the_name_table t1,
my_super_duper_long_table_name_for_descriptions_table t2
WHERE t1.id=t2.id
The reason my query didn't work is probably because I used an INNER join. An inner join only returns values when there is a match between the tables being joined. There could be a problem in your data where one of the three tables did not have a matching value. A LEFT join, on the other hand, will still give you all the records in the first table in your expression even if there is no match in the second table you mention in your join. Your original efforts were using an inner join also even though you did not type "inner join". This is what is called an implicit inner join:
SELECT * FROM table1, table2 WHERE table1.id=table2.id
It's identical to this
SELECT * FROM table1 INNER JOIN table2 ON (table1.id=table2.id)
and these queries will only return records where you have any matches in both tables. If there are no matches, you get no records.
In a LEFT join, you get all the records in table1 whether or not there are any matches in table 2.
SELECT * FROM table1 LEFT JOIN table2 on table1.id=table2.id
If a record in table 1 has no match in table2, all of the table2 values have NULL in them.