-2

How can I create a view in mysql?

Attaching the screenshot as required.

sebenalern
  • 2,515
  • 3
  • 26
  • 36
  • you should better post your table schema as a text but not the image – Alex Jun 08 '16 at 13:00
  • Possible duplicate of [MySQL pivot table](http://stackoverflow.com/questions/7674786/mysql-pivot-table) – Alex Jun 08 '16 at 13:01

1 Answers1

0

You can solve it with conditional aggregation (If I understood correctly table1 contains the ID, and table2 contain the products?

Use this:

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
sagi
  • 40,026
  • 6
  • 59
  • 84