使用不同的表创建视图

时间:2014-08-07 12:10:18

标签: mysql views

create view xyz_view as
select 
    t1.sam_time, 
    t2.hos_id,
    (select t1.STAT_VALUE from table_1 t1 where t1.ID = 6 ) as col_1,
    (select t1.STAT_VALUE from table_1 t1 where t1.ID = 24 ) as col_2,
    (select t1.STAT_VALUE from table_1 t1 where t1.ID = 125 ) as col_3,
    (select t1.STAT_VALUE from table_1 t1 where t1.ID = 143 ) as col_4
from
    table_1 t1, table_2 t2
where 
    t1.entity = t2.hos_id;

这里我收到的错误是: 子查询返回多行mysql

当我设置限制1时,它会在表格中返回一个值。但我想要所有的行 有没有其他选择来获取所有这些数据。

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以在每行的列表中获取所有值。也许这就是你想要的:

select 
    t1.sam_time, 
    t2.hos_id,
    (select group_concat(t1.STAT_VALUE) from table_1 t1 where t1.ID = 6 ) as col_1,
    (select group_concat(t1.STAT_VALUE) from table_1 t1 where t1.ID = 24 ) as col_2,
    (select group_concat(t1.STAT_VALUE) from table_1 t1 where t1.ID = 125 ) as col_3,
    (select group_concat(t1.STAT_VALUE) from table_1 t1 where t1.ID = 143 ) as col_4
from table_1 t1 join
     table_2 t2
     on t1.entity = t2.hos_id;

您还应学会使用正确的join语法,该语法使用join关键字和on关键字表示表格之间的条件。