Advertisement

Author Topic: sql query  (Read 1884 times)

0 Members and 1 Guest are viewing this topic.

npis

  • Guest
sql query
« on: March 27, 2012, 08:01:49 PM »
I have two tables employee and employee_addre ss

employee have name, status, id
john, active, 001
tom, active, 002
josh, inactive, 003
tammy, activce, 004
debbie, active, 005
sammy, active, 006

employee_addre ss have id, address, city, state
002, 123 johnson st, st. paul, mn
005, 456 fifth st, minnneapolis, mn

I want to list all active employees and if exist their address. Output shoud be
john
tom, 123 johnson st, st. paul, mn
tammie
debbie, 456 fifth st, minnneapolis, mn
sammy

I'm doing it with the inner join but it's not giving me the result i want.



Like this post: 0

Adverstisement

tsiehta_goy_vuk

  • Guest
Re: sql query
« Reply #1 on: March 27, 2012, 08:16:12 PM »
I have two tables employee and employee_addre ss

employee have name, status, id
john, active, 001
tom, active, 002
josh, inactive, 003
tammy, activce, 004
debbie, active, 005
sammy, active, 006

employee_addre ss have id, address, city, state
002, 123 johnson st, st. paul, mn
005, 456 fifth st, minnneapolis, mn

I want to list all active employees and if exist their address. Output shoud be
john
tom, 123 johnson st, st. paul, mn
tammie
debbie, 456 fifth st, minnneapolis, mn
sammy

I'm doing it with the inner join but it's not giving me the result i want.

What is your sql statement?



Like this post: 0

npis

  • Guest
Re: sql query
« Reply #2 on: March 27, 2012, 08:42:51 PM »
select employee.name,
employee_addre ss.address,
employee_addre ss.city,
employee_addre ss.state,
employee_addre ss.zip

From employee INNER JOIN employee_addre ss ON employee.id = employee_addre ss.id
where employee.statu s = 'active';

This only gives me the employee whose active and have an address. I want all active employees and address if exist. If not exist just leave address related field blank.



Like this post: 0

npis

  • Guest
Re: sql query
« Reply #3 on: March 28, 2012, 04:05:07 PM »
OTB,

Thanks for the reply. I found it. Instead of INNER JOIN, I had to use LEFT OUTER JOIN.



Like this post: 0

MeetSomeOne

  • Guest
Re: sql query
« Reply #4 on: May 18, 2012, 12:20:09 PM »
use "left outer join".  this would give you your set of data and the other.



Like this post: 0

 

Advertisements