两张表
TableA
Id Name Time
1 张三
2 张三
3 张三
4 张三
5 李四TableB
Id Name Time
1 张三
2 李四
3 李四求当TableA.Name='张三' 和 TableB.Name='张三' 时的记录条数,而不是TableA.Name=TableB.Name 时的记录条数.我写的:select count(*) from TableA,TableB where TableA.Name='张三' and Table.Name='张三'这样查出来的结果是返回4条记录select count(*) from TableA,TableB where TableA.Name='张三' or Table.Name='张三'这样查出来的结果是返回34条记录
我要求查询返回结果应该是5条记录,请问这样的sql应该怎么写?
请高手指教
TableA
Id Name Time
1 张三
2 张三
3 张三
4 张三
5 李四TableB
Id Name Time
1 张三
2 李四
3 李四求当TableA.Name='张三' 和 TableB.Name='张三' 时的记录条数,而不是TableA.Name=TableB.Name 时的记录条数.我写的:select count(*) from TableA,TableB where TableA.Name='张三' and Table.Name='张三'这样查出来的结果是返回4条记录select count(*) from TableA,TableB where TableA.Name='张三' or Table.Name='张三'这样查出来的结果是返回34条记录
我要求查询返回结果应该是5条记录,请问这样的sql应该怎么写?
请高手指教
count(*)
from
(
select * from tableA union all select * from tableB
) t
where Name='张三'
(select * from tablea
union all
select * from tableb)aa
where name='张三'
包含 UNION 运算符的查询表达式中的所有查询都必须在选择列表中包含同样数目的表达式。
(select count(*) from tablea where Name = '张三') + (select count(*) from tableb where Name = '张三')
select
count(*)
from
(
select name from tableA union all select name from tableB
) t
where Name='张三'
insert into tablea values(1 , '张三')
insert into tablea values(2 , '张三')
insert into tablea values(3 , '张三')
insert into tablea values(4 , '张三')
insert into tablea values(5 , '李四')
create table TableB (Id int,Name varchar(10))
insert into tableB values(1 , '张三')
insert into tableB values(2 , '李四')
insert into tableB values(3 , '李四')
goselect
(select count(*) from tablea where Name = '张三') + (select count(*) from tableb where Name = '张三')
/*
-----------
5(所影响的行数为 1 行)
*/select id , name from tablea where Name = '张三'
union all
select id , name from tableb where Name = '张三'
/*
id name
----------- ----------
1 张三
2 张三
3 张三
4 张三
1 张三(所影响的行数为 5 行)
*/drop table tablea , tableb
where Name='张三'select
(select count(*) from tablea where Name = '张三') + (select count(*) from tableb where Name = '张三')谢谢:josy dawugui