如何在2个表之间创建Mysql视图

时间:2014-03-31 19:55:43

标签: mysql sql view relation

我想在一个包含数据的表和另一个包含该字段必须显示的标志的表之间创建一个视图。

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 (此表通过table_name,field_name,entry_id与其他表连接。其他字段为show / not_show标志)

+--------------+------------+----------+------+
| 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   |
+--------------+------------+----------+------+

以下是结果视图。如果标志为0,则字段内容必须为NULL

RESULT_TABLE

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

有任何想法或建议吗?我无法得到它。

3 个答案:

答案 0 :(得分:0)

这是一种方法,您可以连接到表三次,然后使用case语句来确定每列的值:

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';

答案 1 :(得分:0)

如果REFERENCE_TABLE包含每个实体的所有字段的标记,那么您可以使用:

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

或如果省略某些标记,请使用LEFT JOIN。然后省略的标志将被视为0.

答案 2 :(得分:0)

您可以使用其他答案中列出的SELECT - 语句来创建视图,如下所示:

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');