2012-03-19 81 views
0

我有2个表与一些列和另一个表,我想它的行作为视图的列。创建一个3表的视图

table1 
uid|atr1|atr2|atr3 

table2 
uid|col1|col2 

profile_fields(this is a dynamic table, thats why its rows) 
fid|name 

profile_values 
fid|uid|value 

所以最后,我想每个UID那样的视图,1行:用于与它的所有用户数据的每个UID

View 
uid|atr1|atr2|atr3|col1|col2|name(all row names here from profile_Fields)| 

和1个条目行和值

我不明白如何使用profile_fields制作视图,因为它具有行而不是列。它首先在没有这张表的情况下工作,但不能像我上面解释的那样工作。

+0

你可以更具体地说'profile_fields'吗?提供表结构和数据示例。 – Devart 2012-03-19 15:13:18

+0

我认为这不被支持,你可以在这里阅读:select_statement可以从基表或其他视图中选择(http://dev.mysql.com/doc/refman/5.1/en/create-view.html)。 – user1027167 2012-03-19 15:17:30

+0

profile_fields(列fid |名称) 并且有一些行,例如1 | category1,2 | category2,3 | category3等 – heav3n 2012-03-20 11:57:06

回答

0

您应该使用数据透视表机制,例如, -

SELECT pv.*, 
    MAX(IF(pf.field = 'Category1', pv.value, NULL)) AS Category1, 
    MAX(IF(pf.field = 'Category2', pv.value, NULL)) AS Category2, 
    MAX(IF(pf.field = 'Category3', pv.value, NULL)) AS Category3 
FROM profile_values pv 
    LEFT JOIN profile_fields pf 
    ON pv.fid = pf.fid 
GROUP BY uid 

然后将此查询加入到您的表中。