我想根据列中的子环值从不同的数据库中选择结果。 这是我的学生:
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
我的问题是:如何动态地将子字符串连接到数据库名称或列名?
由于
答案 0 :(得分: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)
答案 1 :(得分:0)
您是否尝试使用Case语句检查连接。试着看看这个链接