1

我想根据列中的子环值从不同的数据库中选择结果。这是我的表学生:

Original_student        Other_student
1010173                   1240240
1010173                   1240249

编号中的第 3 位将用于区分数据库。例如。我希望查询是

select original_student, Other_student, month
from student join database-(substring(other_student,3,1).payment 

我的问题是:如何将子字符串动态连接到数据库名称或列名称?

谢谢

4

2 回答 2

0

假设您有一个字段来通过唯一的 id ( id_student) 来识别每个学生,这里有一个便宜的替代方案:

CREATE OR REPLACE VIEW v_student_payment AS
SELECT 0 AS db, payment, id_student FROM database-0
UNION 
SELECT 1 AS db, payment, id_student FROM database-1
UNION 
SELECT 2 AS db, payment, id_student FROM database-2
UNION 
SELECT 3 AS db, payment, id_student FROM database-3
/* here you have to add all databases you're using. There's a little maintenance cost, for if one day there's a new database to be created this view would have to be modified */
;

SELECT 
    original_student, 
    Other_student, 
    month, 
    v.payment
FROM 
    student s
        JOIN v_student_payment v ON v.id_student = s.id_student AND v.db = SUBSTRING(other_student,3,1) 
于 2013-11-07T05:23:39.817 回答
0

您是否尝试使用 Case 语句来检查连接。尝试查看此链接

Join 中的用例语句

于 2013-11-07T05:38:57.323 回答