MySql View基于三个表并转置

时间:2016-06-08 12:58:51

标签: mysql view

如何在mysql中创建视图?

根据需要附加屏幕截图。

1 个答案:

答案 0 :(得分:0)

你可以用条件聚合解决它(如果我理解正确table1包含ID,table2包含产品?

使用此:

CREATE VIEW YourViewName AS
SELECT t.id,
       MAX(CASE WHEN t.product = 'apple' then 'Y' END) as apple,
       MAX(CASE WHEN t.product = 'guava' then 'Y' END) as guava,
       MAX(CASE WHEN t.product = 'mango' then 'Y' END) as mango,
       MAX(CASE WHEN t.product = 'cherry' then 'Y' END) as cherry,
       MAX(CASE WHEN t.product = 'carrot' then 'Y' END) as carrot
FROM(SELECT t1.id,t3.product FROM Table1 t1
     LEFT JOIN Table3 t3
      ON(t1.id = t3.id)) t
GROUP BY t.id