select count(*) from的问题

来源:百度知道 编辑:UC知道 时间:2024/06/03 16:13:59
想要同是查询两个表的行数怎么写
select count(*) as count,count(*) as count1 from table1,table2
这样不行,我试过了
你的方法我也试过了,还是不行,得出的两个结果都是相同的,而且不对

一劳永逸的搞法
declare @tbl nvarchar(50)

Create table #tblSpace(
Name nvarchar(40),
Rows int,
reserved varchar(18),
Data varchar(18),
index_size varchar(18),
Unused varchar(18)
)

declare cur cursor for
Select name from sysobjects where xtype='U' and name='table1' or name = 'table2'

open cur
Fetch next from cur into @tbl
while @@fetch_status=0
begin
Insert into #tblSpace
EXEC sp_spaceused @tbl

Fetch next from cur into @tbl
end
close cur
deallocate cur
go

select name as 表名,rows as 行数 from #tblSpace
drop table #tblSpace

select count(table1.id) as count1,count(table2.id) as count2 from table1,table2

此时不能使用 * 了。当然id也可以换成表中的任何一个字段名。

上述语句的确是错误的。不过,想了很多方法,也到网上狂搜了一阵,都没有什么合适的方法可以通过一句话得到两个表的行数。

建议还是通过两条sql语句执行吧。