目前我有两个表中定义的用户结构: 用户(id,name)和UserParams(user_id,param,value) 因此,当前的数据看起来如下:
User:
id, name
1, "John"
2, "Bob"
UserParams:
user_id, param, value
1, "Address", "Kilmainham str"
1, "Phone", "+12345678"
我想重构数据库,因此用户将有明确的列地址,电话等:
New User structure:
id, name, address, phone
1, "John", "Kilmainham str", "+12345678"
2, "Bob", "Orc's cave", "just speak up"
糟糕的是,数据库有许多存储过程,在单条连线中很难重构。所以,我希望有一个视图或存储函数,它将模仿旧的“UserParams”行为。
SELECT * FROM UserParams WHERE user_id=1;
1, "Address", "Kilmainham str"
1, "Phone", "+12345678"
这样做有什么办法吗?感谢。
答案 0 :(得分:1)
CREATE VIEW UserParams (user_id, param, value)
AS SELECT id, "Address" , address
FROM New_User
UNION
SELECT id, "Phone", phone
FROM New_User