2014-01-23 28 views
0

目前我有兩個表中定義的用戶結構: 用戶(id,名稱)和UserParams(user_id,參數,值) 因此,當前數據的方式如下:在MySQL中的模仿表(查看或存儲功能)

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" 

它是沒有辦法做到這一點?謝謝。

回答

1
CREATE VIEW UserParams (user_id, param, value) 
AS SELECT id, "Address" , address 
FROM New_User 
UNION 
SELECT id, "Phone", phone 
FROM New_User 
+0

正是我所需要的,謝謝! – ZAN