select *,(select count(*) from tb where id=a.id)ct from tb a
create view v_name as begin select *,count(*)over() as [count] from tb endselect * from v_name
不是 楼上你看我写的create view View_Toc ("Toc_Idn","Parent_Idn","Title","Link","DescendantCount","Product_Idn") as select Toc.Toc_Idn as "Toc_Idn", Toc.Parent_Idn as "Parent_Idn", Toc.Title as "Title", Toc.Link as "Link",
Toc."Product_Idn" as "Product_Idn" from Toc我点 的地方 就是要填的,需要填的是 从 toc 表 某个字段的count..难道可以?create view View_Toc ("Toc_Idn","Parent_Idn","Title","Link","DescendantCount","Product_Idn") as select Toc.Toc_Idn as "Toc_Idn", Toc.Parent_Idn as "Parent_Idn", Toc.Title as "Title", Toc.Link as "Link", (SELECT COUNT(ZIDUAN) FROM TOC) AS DescendantCount, Toc."Product_Idn" as "Product_Idn" from Toc不行吧
create view View_Toc ("Toc_Idn","Parent_Idn","Title","Link","DescendantCount","Product_Idn") as select Toc.Toc_Idn as "Toc_Idn", Toc.Parent_Idn as "Parent_Idn", Toc.Title as "Title", Toc.Link as "Link", COUNT(ZIDUAN) over() AS DescendantCount, Toc."Product_Idn" as "Product_Idn" from Toc
不过,貌似你不是MSSQL吧,怎么弄那么多双引号!
create view View_Toc as select Toc.Toc_Idn as "Toc_Idn", Toc.Parent_Idn as "Parent_Idn", Toc.Title as "Title", Toc.Link as "Link", (SELECT COUNT(ZIDUAN) FROM TOC) AS DescendantCount, Toc."Product_Idn" as "Product_Idn" from Toc go
谢谢两位,但是我的问题没这么简单 我给表结构吧表的结构 ID paerntId .... 现在视图的结构要是 ID paerntId DescendantCount ... 加粗的是多出来的并且他的值是 select count(paerntId) from table where paerntId = ID就是我要判断这个ID 下 有多少条记录 是 paerntId = ID 的 这个值 对应DescendantCount
没关系 我的是oracle 的写法 sqlserver 也支持的
create view View_Toc as select Toc.Toc_Idn as "Toc_Idn", Toc.Parent_Idn as "Parent_Idn", Toc.Title as "Title", Toc.Link as "Link", (SELECT COUNT(ZIDUAN) FROM TOC where paerntId=a.paerntId) AS DescendantCount, Toc."Product_Idn" as "Product_Idn" from Toc a go
create view View_Toc ("Toc_Idn","Parent_Idn","Title","Link","DescendantCount","Product_Idn") as select Toc.Toc_Idn as "Toc_Idn", Toc.Parent_Idn as "Parent_Idn", Toc.Title as "Title", Toc.Link as "Link", (select COUNT(Parent_Idn) from Toc where Toc_Idn = 2) as "DescendantCount", Toc."Product_Idn" as "Product_Idn" from Toc像我写的 但是上面的2 是ID 应该是个变量 感觉不行
晕菜,1楼不是给你方法了么.select a, b, c, (select count(*) from tb where id=a.id)d, e, f from tb a 在表的最后添加了一个别名 a,新增列统计与这条记录相同的id.
我就这样写的 create view View_Toc ("Toc_Idn","Parent_Idn","Title","Link","DescendantCount","Product_Idn") as select a.Toc_Idn as "Toc_Idn", a.Parent_Idn as "Parent_Idn", a.Title as "Title", a.Link as "Link", (select COUNT(*) from Toc where Toc_Idn = a.Parent_Idn) as "DescendantCount", a."Product_Idn" as "Product_Idn" from Toc a
你看结果select * from View_Toc select * from toc2 0 Configuration /Content/Windows/LPs/zz_lp_toolbox_configuration.htm 0 22 0 Configuration /Content/Windows/LPs/zz_lp_toolbox_configuration.htm 27 2表里是27 视图里是0
select a.Toc_Idn as "Toc_Idn", a.Parent_Idn as "Parent_Idn", a.Title as "Title", a.Link as "Link", b.CC as "DescendantCount", a."Product_Idn" as "Product_Idn" from Toc a LEFT JOIN (SELECT Toc_Idn, COUNT(Toc_Idn) AS "CC" FROM Toc GROUP BY Toc_Idn) b ON a.Parent_Idn=b.CC
as
begin
select *,count(*)over() as [count] from tb
endselect * from v_name
as
select Toc.Toc_Idn as "Toc_Idn",
Toc.Parent_Idn as "Parent_Idn",
Toc.Title as "Title",
Toc.Link as "Link",
Toc."Product_Idn" as "Product_Idn"
from Toc我点 的地方 就是要填的,需要填的是 从 toc 表 某个字段的count..难道可以?create view View_Toc ("Toc_Idn","Parent_Idn","Title","Link","DescendantCount","Product_Idn")
as
select Toc.Toc_Idn as "Toc_Idn",
Toc.Parent_Idn as "Parent_Idn",
Toc.Title as "Title",
Toc.Link as "Link",
(SELECT COUNT(ZIDUAN) FROM TOC) AS DescendantCount,
Toc."Product_Idn" as "Product_Idn"
from Toc不行吧
as
select Toc.Toc_Idn as "Toc_Idn",
Toc.Parent_Idn as "Parent_Idn",
Toc.Title as "Title",
Toc.Link as "Link",
COUNT(ZIDUAN) over() AS DescendantCount,
Toc."Product_Idn" as "Product_Idn"
from Toc
as
select Toc.Toc_Idn as "Toc_Idn",
Toc.Parent_Idn as "Parent_Idn",
Toc.Title as "Title",
Toc.Link as "Link",
(SELECT COUNT(ZIDUAN) FROM TOC) AS DescendantCount,
Toc."Product_Idn" as "Product_Idn"
from Toc
go
我给表结构吧表的结构 ID paerntId ....
现在视图的结构要是 ID paerntId DescendantCount ...
加粗的是多出来的并且他的值是 select count(paerntId) from table where paerntId = ID就是我要判断这个ID 下 有多少条记录 是 paerntId = ID 的 这个值 对应DescendantCount
as
select Toc.Toc_Idn as "Toc_Idn",
Toc.Parent_Idn as "Parent_Idn",
Toc.Title as "Title",
Toc.Link as "Link",
(SELECT COUNT(ZIDUAN) FROM TOC where paerntId=a.paerntId) AS DescendantCount,
Toc."Product_Idn" as "Product_Idn"
from Toc a
go
as
select Toc.Toc_Idn as "Toc_Idn",
Toc.Parent_Idn as "Parent_Idn",
Toc.Title as "Title",
Toc.Link as "Link",
(select COUNT(Parent_Idn) from Toc where Toc_Idn = 2) as "DescendantCount",
Toc."Product_Idn" as "Product_Idn"
from Toc像我写的 但是上面的2 是ID 应该是个变量 感觉不行
a,
b,
c,
(select count(*) from tb where id=a.id)d,
e,
f
from tb a
在表的最后添加了一个别名 a,新增列统计与这条记录相同的id.
create view View_Toc ("Toc_Idn","Parent_Idn","Title","Link","DescendantCount","Product_Idn")
as
select a.Toc_Idn as "Toc_Idn",
a.Parent_Idn as "Parent_Idn",
a.Title as "Title",
a.Link as "Link",
(select COUNT(*) from Toc where Toc_Idn = a.Parent_Idn) as "DescendantCount",
a."Product_Idn" as "Product_Idn"
from Toc a
select * from toc2 0 Configuration /Content/Windows/LPs/zz_lp_toolbox_configuration.htm 0 22 0 Configuration /Content/Windows/LPs/zz_lp_toolbox_configuration.htm 27 2表里是27 视图里是0
a.Parent_Idn as "Parent_Idn",
a.Title as "Title",
a.Link as "Link",
b.CC as "DescendantCount",
a."Product_Idn" as "Product_Idn"
from Toc a
LEFT JOIN
(SELECT Toc_Idn, COUNT(Toc_Idn) AS "CC" FROM Toc GROUP BY Toc_Idn) b
ON a.Parent_Idn=b.CC