开发者

Creating a matrix view from relational db tables

开发者 https://www.devze.com 2023-03-31 18:16 出处:网络
I\'m asked to create a matrix data table using some MYSQL tables. My questions is if it\'s possible to create a matrix view of this data.

I'm asked to create a matrix data table using some MYSQL tables. My questions is if it's possible to create a matrix view of this data.

开发者_如何学JAVA
   MAIN_TABLE
   id
   name

   [Sample data]
   1, Nisse
   2, Joe

   PROP_TABLE
   id,
   key
   value

   [Sample data]
   1, Key1, Value1
   2, Key2, Value2

Now is it possible to use a query or view to retrieve the following structured data?

   ID, NAME, KEY1, KEY2
   1, Nisse, Value1, null
   2, Joe, null, Value2 


SELECT m.id AS ID,
       m.name AS NAME,
       IF (p.key = 'Key1', p.value, NULL) AS KEY1,
       IF (p.key = 'Key2', p.value, NULL) AS KEY2,
FROM MAIN_TABLE as m
LEFT JOIN PROP_TABLE AS p ON p.id = m.id

(I've not tested it.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号