我的mysql数据库中有A和B两个表。
create A (
id int primary auto_increment,
....
);
create B (
id int,
status int default 0, /*only between 0 and 2*/
foreign key (id) references A(id) ,
index `index_test`(id, status)
);
现在,我想从A和B获取信息视图。
create view info
as
select A.id, statistics.status, statistics.status_statistics as ss
from A
left outer join (
select id, status, count(*) as status_statistics
from B
group by(B.id, B.status)
) as 'statistics' on (A.id = statistics.id);
但是,这将获得多行结果而不是那一行。如何获得一个?
例如:
table A:
id
--
1
2
table B:
id status
------------
1 0
1 1
1 2
1 0
2 1
我使用上面的方法,我会得到:
id status ss
-----------------
1 0 2
1 1 1
1 2 1
2 1 1
Howerver,我想得到:
id | column_name_one | column_name_two | column_name_three
------------------------------------------------------------------------------------------
1 2 1 1
2 0 1 0