2

I have a statement like this:

select lastname,firstname,email,floorid 
from employee 
where locationid=1 
    and (statusid=1 or statusid=3) 
order by floorid,lastname,firstname,email

The problem is the column floorid. The result of this query is showing the id of the floors.

There is this table called floor (has like 30 rows), which has columns id and floornumber. The floorid (in above statement) values match the id of the table floor.

I want the above query to switch the floorid values into the associated values of the floornumber column in the floor table.

Can anyone show me how to do this please? I am using Microsoft sql server 2008 r2.

I am new to sql and I need a clear and understandable method if possible.

1
  • 3
    Instead of giving you just a straight answer, let me just suggest you read up on joins. The strength in databases isn't in storing data, it's being able to create related data that you can then retrieve. In order to retrieve related data, you join on whatever columns/criteria you want. This is one of the fundamental aspects of the RDBMS (R = Relational) so it's really just a matter of becoming familiar with the tools available to you (I'd recommend reading some tutorials online, possibly a book or two). Commented Aug 1, 2012 at 15:08

3 Answers 3

5
select   lastname,
         firstname,
         email,
         floor.floornumber
from     employee
inner join floor on floor.id = employee.floorid
where    locationid = 1
         and (statusid = 1 or statusid = 3)
order by floorid, lastname, firstname, email
1

You have to do a simple join where you check, if the floorid matches the id of your floor table. Then you use the floornumber of the table floor.

select a.lastname,a.firstname,a.email,b.floornumber 
from employee a
join floor b on a.floorid = b.id
where a.locationid=1 and (a.statusid=1 or a.statusid=3) 
order by a.floorid,a.lastname,a.firstname,a.email
3
  • @omega you should refer to my answer if you need more information about how this worked.
    – phadaphunk
    Commented Aug 1, 2012 at 15:20
  • I have another problem, if the floorid value in employee table is NULL, then that row gets skipped because there is no id called NULL in the floor table. How can I get this included in the query where the value should just remain NULL?
    – omega
    Commented Aug 1, 2012 at 16:02
  • use "left join" to include lines with null in floorid column
    – mykola
    Commented Aug 1, 2012 at 19:23
1

You need to use a join.
This will join the two tables on a certain field.
This way you can SELECTcolumns 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.