简体   繁体   中英

How to create a Mysql View between 2 tables

I want to create a view between a table that has data and another table that has a flag that the field must show or not.

TABLE_EXAMPLE

+---------+---------+---------+-----------------+
| id      | field_1 | field_2 | field_3         |
+---------+---------+---------+-----------------+
|   1     | test    | 500     | another content |
+---------+---------+---------+-----------------+
|   2     | blah    | 800     | text_lorem      |
+---------+---------+---------+-----------------+
|   3     | hi!     | 100     | lorem_impsum    |
+---------+---------+---------+-----------------+

REFERENCE_TABLE (This table is joined with the other table by table_name, field_name,entry_id. The other field is show/not_show flag)

+--------------+------------+----------+------+
| table_name   | field_name | entry_id | show |
+--------------+------------+----------+------+
| TABLE_EXAMPLE| field_1    | 1        |  0   |
+--------------+------------+----------+------+
| TABLE_EXAMPLE| field_2    | 1        |  1   |
+--------------+------------+----------+------+
| TABLE_EXAMPLE| field_3    | 1        |  0   |
+--------------+------------+----------+------+
| TABLE_EXAMPLE| field_1    | 2        |  1   |
+--------------+------------+----------+------+
| TABLE_EXAMPLE| field_2    | 2        |  0   |
+--------------+------------+----------+------+
| TABLE_EXAMPLE| field_3    | 2        |  1   |
+--------------+------------+----------+------+
| TABLE_EXAMPLE| field_1    | 3        |  1   |
+--------------+------------+----------+------+
| TABLE_EXAMPLE| field_2    | 3        |  1   |
+--------------+------------+----------+------+
| TABLE_EXAMPLE| field_3    | 3        |  0   |
+--------------+------------+----------+------+

Here is the resultant view. In case the flag is 0 the field content must be NULL

RESULT_TABLE

+---------+---------+---------+-----------------+
| id      | field_1 | field_2 | field_3         |
+---------+---------+---------+-----------------+
|   1     | NULL    | 500     | NULL            |
+---------+---------+---------+-----------------+
|   2     | blah    | NULL    | text_lorem      |
+---------+---------+---------+-----------------+
|   3     | hi!     | 100     | NULL            |
+---------+---------+---------+-----------------+

Any idea or suggestion? I couldn't get it.

Here is one method where you join to the table three times and then use a case statement to determine what the value is for each column:

select e.id,
       (case when r1.show then e.field_1 end) as field_1,
       (case when r2.show then e.field_2 end) as field_2
       (case when r3.show then e.field_3 end) as field_3
from table_example e left join
     reference_table r1
     on r1.table_name = 'table_name' and r1.entry_id = e.id and r1.column_name = 'field_1' left join      
     reference_table r2
     on r2.table_name = 'table_name' and r2.entry_id = e.id and r2.column_name = 'field_2 left join     
     reference_table r3
     on r3.table_name = 'table_name' and r3.entry_id = e.id and r3.column_name = 'field_3';

If the REFERENCE_TABLE has flags for all fields of each entity, then you can use this:

SELECT
  T1.ID,
  MAX(CASE WHEN T2.field_name = 'field_1' AND T2.show = 1 THEN T1.field_1 END) field_1,
  MAX(CASE WHEN T2.field_name = 'field_2' AND T2.show = 1 THEN T1.field_2 END) field_2,
  MAX(CASE WHEN T2.field_name = 'field_3' AND T2.show = 1 THEN T1.field_3 END) field_3,
FROM TABLE_EXAMPLE T1
  JOIN REFERENCE_TABLE T2
    ON T1.id = T2.entity_id
WHERE T2.table_name = 'TABLE_EXAMPLE'
GROUP BY T1.ID

or use LEFT JOIN if you omit some flags. Then the omitted flags will treats as 0.

You can use the SELECT -statements listed in other answers to create the view as follows:

CREATE VIEW v1 AS 
select id, if(f1.shw=1,field_1,null) field_1, if(f2.shw=1,field_2,null) field_2,     if(f3.shw=1,field_3,null) field_3
 from TABLE_EXAMPLE
  left join REFERENCE_TABLE as f1 on (f1.entry_id=TABLE_EXAMPLE.id and f1.field_name="field_1" and f1.table_name='TABLE_EXAMPLE')
  left join REFERENCE_TABLE as f2 on (f2.entry_id=TABLE_EXAMPLE.id and f2.field_name="field_2" and f2.table_name='TABLE_EXAMPLE')
  left join REFERENCE_TABLE as f3 on (f3.entry_id=TABLE_EXAMPLE.id and f3.field_name="field_3" and f3.table_name='TABLE_EXAMPLE');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM