You need to use a join.
This will join the two tables on a certain field.
This way you can SELECT
columns from more than one table at the time.
When you join two tables you have to specify on which column you want to join them.
In your example, you'd have to do this:
from employee join floor on employee.floorid = floor.id
Since you are new to SQL you must know a few things. With the other enaswers you have on this question, people use aliases
instead of repeating the table name.
from employee a join floor b
means that from now on the table employee will be known as a
and the table floor as b
. This is really usefull when you have a lot of joins to do.
Now let's say both table have a column name
. In your select you have to say from which table you want to pick the column name. If you only write this
SELECT name from Employee a join floor b on a.id = b.id
the compiler won't understand from which table you want to get the column name
. You would have to specify it like this :
SELECT Employee.name from Employee a join floor b on a.id = b.id
or if you prefer with aliases :
SELECT a.name from Employee a join floor b on a.id = b.id
Finally there are many type of joins.
- Inner join ( what you are using because simply typing
Join
will refer to an inner join.
- Left outer join
- Right outer join
- Self join
- ...
To should refer to this article about joins to know how to use them correctly.
Hope this helps.