|
查看: 888|回复: 12
|
SQL:join 2 table[已解决]
[复制链接]
|
|
|
谢谢各位大大的帮忙,给了小弟很多启发
小弟终于做到了,到不懂是不是最好的,但总算有了我要的东西
select * from
(select rownum,personnel_number,lastname,firstname, Email,Position_title,Business_segment,emp_location end from table1) a
left join table2 b on
a.emp_location = b.location
and a.new_cost_center = b.cost_center
resultable是我拿到的是每个员工会有自己的direct manager according to 他们的cost center 和location
----------------------------------------------------------------------------------------------------------------------
小弟学术不精, 做了很久,还是做不到
我有两个table, 我要把他们join 起来
table (一),是这样的
它有location, hr_manager_name,cost_center
location 会一样,hr_manager_name也会一样,不一样就是cost_center
table(二),是这样的
它是employee detail的table
每个employee 一定有location(所属地区)
我要的是
做一个table 出来(可能用join)
我要每个employee 都显示出他们的 hr_manager_name
但我遇到的问题是我table(一)里的location 都会重复的,sql 应该是不懂要拿那一个
所以, 我用distinct
但用了, 我用不会join 了
以下的是我目前做到的:
table(一):
select distinct(location), hr_local_admin
from myDataBase_local_admin
table(二):
select emp_location from myDataBase_employee
请指教。。。
谢谢各位。。
[ 本帖最后由 心的太平洋 于 8-9-2008 03:26 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 2-9-2008 05:03 PM
|
显示全部楼层
SELECT DISTINCT A.location, A.hr_manager_name,A.hr_local_admin
from myDataBase_local_admin A, myDataBase_employee B
WHERE A.location = B.emp_location |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 2-9-2008 05:13 PM
|
显示全部楼层
回复 2# poweryou2 的帖子
谢谢你, poweryou2
我找到方向了。。。
只是我拿到的result 是几个employee 罢了
我要的是全部employee,所以冒昧地请问
是不是把其中的order倒转就能拿到了吗?
谢谢指教 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 2-9-2008 05:29 PM
|
显示全部楼层
回复 3# 心的太平洋 的帖子
SELECT DISTINCT (A.location), A.hr_local_admin,b.emp_location, b.*
from myDataBase_local_admin A, myDataBase_employee B
WHERE A.location = B.emp_location
如果我这样写, 是不是全部的employee data我全拿, 加上他们的所属的location?
对吗? |
|
|
|
|
|
|
|
|
|
|
发表于 2-9-2008 09:16 PM
|
显示全部楼层
原帖由 心的太平洋 于 2-9-2008 05:29 PM 发表 
SELECT DISTINCT (A.location), A.hr_local_admin,b.emp_location, b.*
from myDataBase_local_admin A, myDataBase_employee B
WHERE A.location = B.emp_location
如果我这样写, 是不是全部的employee data我全 ...
你。。。。
test过吗??? |
|
|
|
|
|
|
|
|
|
|
发表于 2-9-2008 10:04 PM
|
显示全部楼层
|
写SQL前应该明白table是怎样连接起来的吧?照你的解说,用location来找manager_name就是大错特错了。两个location都有可能会有复制,不是primary key。你需要找一个primary key的连接。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 3-9-2008 09:06 AM
|
显示全部楼层
回复 6# CryoWraith 的帖子
请问大家有办法帮我决解吗?
我需要每个人都有manager_name
谢谢 |
|
|
|
|
|
|
|
|
|
|
发表于 3-9-2008 10:29 AM
|
显示全部楼层
select A.location, A.hr_local_admin,B.emp_location from myDataBase_local_admin A inner join myDataBase_employee B
on A.location = B.emp_location
order by A.location |
|
|
|
|
|
|
|
|
|
|
发表于 3-9-2008 09:29 PM
|
显示全部楼层
|
问一下,你是用什么database的?有办法把database diagram / class diagram 放上来吗? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 4-9-2008 03:34 PM
|
显示全部楼层
回复 9# CryoWraith 的帖子
我是用oracle 的
不好意思, 小弟学浅,什么是database diagram / class diagram ? |
|
|
|
|
|
|
|
|
|
|
发表于 5-9-2008 03:03 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 5-9-2008 04:12 PM
|
显示全部楼层
先要请问LZ
一个 employee 有一个location 罢了。
问题:
是 一个location 有很多hr_manager_name = 一个employee有很多hr_manager_name
还是一个location 有一个hr_manager_name = 一个employee只有一个hr_manager_name
如果你要的result 是第二(一个employee 只有一个hr_manager_name ),那你的table 1 design 就错了,像六楼说的那样。应该把 cost_center 分开,这样table 1 就有primary key。
原帖由 CryoWraith 于 2-9-2008 10:04 PM 发表 写SQL前应该明白table是怎样连接起来的吧?照你的解说,用location来找manager_name就是大错特错了。两个location都有可能会有复制,不是primary key。你需要找一个primary key的连接。
对了,还有第三的情况:
一个 employee 只有一个location
一个 location 有很多hr_manager_name
一个 employee 只有一个hr_manager_name
这样的话 location 就不能当 table 2 的 primary key。
如果LZ 能解释table 的 relation 和你要的 result。其他大大才能帮到你。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 8-9-2008 03:16 PM
|
显示全部楼层
回复 12# cutan 的帖子
先谢谢cutan..
是我的解释能力差,误解了大家
小弟要得是,assign 一个direct_manager 给每个员工,只要每个员工有的cost_center,他们就有direct_manager
所以照cutan的说法,
是一个location(我所谓的cost_center) 有一个hr_manager_name = 一个employee只有一个hr_manager_name |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|