mysql - 子查询
mysql - subqueries
我有这个table
我想要并且可以让它看起来像这样
我使用的查询是这样的
{
SELECT Cat,Reg,Branch, Mnth, Qty
FROM
(
SELECT Cat,Reg,Branch, 'Jan' Mnth, Jan Qty
FROM BQV
WHERE Jan > 0
UNION ALL
SELECT Cat,Reg,Branch, 'Feb', Feb
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Mar', Mar
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Apr', Apr
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'May', May
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Jun', Jun
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Jul', Jul
FROM BQV
WHERE Feb > 0
UNION ALL
SELECT Cat,Reg,Branch, 'Aug', Aug
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Sept', Sep
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Oct', Octo
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Nov', Nov
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Dec', Dece
FROM BQV
) QV
}
我可以通过视图或存储过程或其他方式实现吗??
我的 MySQL ver 是 5.5,它不能在视图中有子查询,我不能升级我的数据库,因为我不能承受任何损失或错误。
您不需要 Derived Table(它被 MySQL 错误地命名为 Subquery) , 只需删除它:
CREATE VIEW foo AS
SELECT Cat,Reg,Branch, 'Jan' AS Mnth, Jan AS Qty
FROM BQV
WHERE Jan > 0
UNION ALL
SELECT Cat,Reg,Branch, 'Feb', Feb
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Mar', Mar
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Apr', Apr
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'May', May
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Jun', Jun
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Jul', Jul
FROM BQV
WHERE Feb > 0
UNION ALL
SELECT Cat,Reg,Branch, 'Aug', Aug
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Sept', Sep
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Oct', Octo
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Nov', Nov
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Dec', Dece
FROM BQV
另一个解决方案是基于 CROSS JOIN
到第二个 table 存储月份名称 'Jan' 到 'Dec':
create table months ( mnth char(3));
insert into months values ('Jan'),('Feb'),...
select Cat,Reg,Branch, months.mnth,
case mnth
when 'Jan' then Jan
when 'Feb' then Feb
...
when 'Dec' then Dece
end
from BQV CROSS JOIN months
下面的视图完美地满足了我的需要。
创建视图 scheme-test
。trybqv1
作为
SELECT Cat,Reg,Branch, 'Jan' Mnth,Jan 数量
来自 BQV A
联合所有
SELECT Cat、Reg、Branch,'Feb',2 月
来自 BQV
联合所有
SELECT Cat、Reg、Branch,'Mar',三月
来自 BQV
联合所有
SELECT Cat、Reg、Branch,'Apr',四月
来自 BQV
联合所有
SELECT Cat、Reg、Branch,'May',五月
来自 BQV
联合所有
SELECT Cat,Reg,Branch, 'Jun', 君
来自 BQV
联合所有
SELECT Cat、Reg、Branch,'Jul',7 月
来自 BQV
联合所有
SELECT Cat、Reg、Branch,'Aug',8 月
来自 BQV
联合所有
SELECT Cat、Reg、Branch,'Sept',9 月
来自 BQV
联合所有
SELECT Cat、Reg、Branch、'Oct'、Octo
来自 BQV
联合所有
SELECT Cat、Reg、Branch,'Nov',11 月
来自 BQV
联合所有
SELECT Cat、Reg、Branch、'Dec'、Dece
来自 BQV
我有这个table
我想要并且可以让它看起来像这样
我使用的查询是这样的 {
SELECT Cat,Reg,Branch, Mnth, Qty
FROM
(
SELECT Cat,Reg,Branch, 'Jan' Mnth, Jan Qty
FROM BQV
WHERE Jan > 0
UNION ALL
SELECT Cat,Reg,Branch, 'Feb', Feb
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Mar', Mar
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Apr', Apr
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'May', May
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Jun', Jun
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Jul', Jul
FROM BQV
WHERE Feb > 0
UNION ALL
SELECT Cat,Reg,Branch, 'Aug', Aug
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Sept', Sep
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Oct', Octo
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Nov', Nov
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Dec', Dece
FROM BQV
) QV
}
我可以通过视图或存储过程或其他方式实现吗?? 我的 MySQL ver 是 5.5,它不能在视图中有子查询,我不能升级我的数据库,因为我不能承受任何损失或错误。
您不需要 Derived Table(它被 MySQL 错误地命名为 Subquery) , 只需删除它:
CREATE VIEW foo AS
SELECT Cat,Reg,Branch, 'Jan' AS Mnth, Jan AS Qty
FROM BQV
WHERE Jan > 0
UNION ALL
SELECT Cat,Reg,Branch, 'Feb', Feb
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Mar', Mar
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Apr', Apr
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'May', May
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Jun', Jun
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Jul', Jul
FROM BQV
WHERE Feb > 0
UNION ALL
SELECT Cat,Reg,Branch, 'Aug', Aug
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Sept', Sep
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Oct', Octo
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Nov', Nov
FROM BQV
UNION ALL
SELECT Cat,Reg,Branch, 'Dec', Dece
FROM BQV
另一个解决方案是基于 CROSS JOIN
到第二个 table 存储月份名称 'Jan' 到 'Dec':
create table months ( mnth char(3));
insert into months values ('Jan'),('Feb'),...
select Cat,Reg,Branch, months.mnth,
case mnth
when 'Jan' then Jan
when 'Feb' then Feb
...
when 'Dec' then Dece
end
from BQV CROSS JOIN months
下面的视图完美地满足了我的需要。
创建视图 scheme-test
。trybqv1
作为
SELECT Cat,Reg,Branch, 'Jan' Mnth,Jan 数量 来自 BQV A 联合所有 SELECT Cat、Reg、Branch,'Feb',2 月 来自 BQV
联合所有 SELECT Cat、Reg、Branch,'Mar',三月 来自 BQV
联合所有 SELECT Cat、Reg、Branch,'Apr',四月 来自 BQV
联合所有 SELECT Cat、Reg、Branch,'May',五月 来自 BQV
联合所有 SELECT Cat,Reg,Branch, 'Jun', 君 来自 BQV
联合所有 SELECT Cat、Reg、Branch,'Jul',7 月 来自 BQV
联合所有 SELECT Cat、Reg、Branch,'Aug',8 月 来自 BQV
联合所有 SELECT Cat、Reg、Branch,'Sept',9 月 来自 BQV
联合所有 SELECT Cat、Reg、Branch、'Oct'、Octo 来自 BQV
联合所有 SELECT Cat、Reg、Branch,'Nov',11 月 来自 BQV
联合所有 SELECT Cat、Reg、Branch、'Dec'、Dece 来自 BQV