这种情况如何查询好些?
table1
id column11 column12
1 4 2
2 5 3
table2
id column21 column22
1 1 2
2 2 3
3 3 5
4 4 7
5 5 8
table3
id column31 column32
1 1 4
2 2 9
3 3 0
其中table2中的column21唯一,且对应于table1中的column11或table3中的column31
要求得到结果:
id table2id column21 column22 TmpColumn
1 1 1 2 4
2 2 2 3 9
3 3 3 5 0
4 4 4 7 2
5 5 5 8 3
其中id为从1开始的自增字段
PS:我目前解决方法是在table2中增加一个字段btable用来标识该记录在table3或是table1中,1表示在table3中,0表示在table1中
现table2情况如下
id column21 column22 btable
1 1 2 1
2 2 3 1
3 3 5 1
4 4 7 0
5 5 8 0
select table2.id as table2id,column21,column22,
tmpColumn = case when btable = 1 then (select column32 from table3 where column31 = table2.column1) else (select column12 from table1 where column11 = table2.column1) end from table2
(id还没想到怎么去解决)
有没有好些的办法...
多谢,不胜感激!!