MySQL 2交叉表视图不具有1 = 1关系成一个视图

时间:2018-09-23 12:04:10

标签: php mysql max case

我有一个包含四列subfield_id, submission_id, field_id, value的mysql表

我提出了两个看法:

1。

SELECT submission_id, 
       MAX( CASE field_id WHEN '1' THEN value ELSE '0' END ) Usernumber,
       MAX( CASE field_id WHEN '2' THEN value ELSE '0' END ) Company, 
       MAX( CASE field_id WHEN '3' THEN value ELSE '0' END ) Zipp, 
FROM table
GROUP BY submission_id

结果表列:Submission_id, Usernumber, Company, Zipp

2。

SELECT submission_id, 
       MAX( CASE field_id WHEN '4' THEN value ELSE '0' END ) Usernumber1, 
       MAX( CASE field_id WHEN '5' THEN value ELSE '0' END ) Partnernumber 
FROM table 
GROUP BY submission_id

结果表列:Submission_id, Usernumber1, Partnernumber

UsernumberUsernumber1是相同的数字

如何使用以下各列进行查看?

Usernumber, Company,  Zipp, Partnernumber

1 个答案:

答案 0 :(得分:0)

只需在单个查询中写下这个内容即可

SELECT submission_id, 
       MAX( CASE field_id WHEN '1' THEN value ELSE '0' END ) as Usernumber,
       MAX( CASE field_id WHEN '2' THEN value ELSE '0' END ) as Company, 
       MAX( CASE field_id WHEN '3' THEN value ELSE '0' END ) as Zipp, 
       MAX( CASE field_id WHEN '5' THEN value ELSE '0' END ) as Partnernumber 
FROM table
GROUP BY submission_id;

视图不会真正帮助您进行此查询。

编辑:

根据您的评论:

select *
from view1 v1 join
     view2 v2
     using (Usernumber);