--有如下三张表
create table tablea
(
type int default 0,
value nvarchar(10)
)
create table tableb
(
id int, -- 主键
le int default 0,
)
create table tablec
(
id int, -- 主键
bid int, -- 对应tableb中的id
value nvarchar(10)
)数据
tablea tableb tablec
type value id le id bid value
0 abc 1 1 1 1 abc
-1 asdf 2 1 2 1 def
0 345 3 2 3 1 aaa
-1 dddd 4 2 abc
5 2 asdf
6 2 345
7 2 fff
8 3 sdf
9 3 dddd
--要求查出来的数据为 (说明,如果tablec.value在tablea.value中找到,则type为tablea.type,否则为tableb.le)
bablec.id tableb.bid type value
1 1 0 abc
2 1 1 def
3 1 1 aaa
4 2 0 abc
5 2 -1 asdf
6 2 0 345
7 2 1 fff
8 3 2 sdf
9 3 -1 ddd
不知大家能看懂不
isnull(tablea.type,tableb.le) as type,tablec.value
from tablec left join tablea on tablec.value=tablea.value
left join tableb on tablec.bid=tableb.id
c.* isnull(a.type,b.le) as type,
from
tablec c
left join
tablea a
on
c.value=a.value
left join
tableb b
on
c.bid=b.id
c.*,isnull(a.type,b.le) as type,
from
tablec c
left join
tablea a
on
c.value=a.value
left join
tableb b
on
c.bid=b.id
(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
这位任兄,不好意思,这并不是作业,呵呵,因为我对SQL并不太熟,我是搞C的.呵呵...