首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎的实现这个SQL语句

2012-11-03 
怎样实现这个SQL语句?表A有列id name code_name code1 张三 aaa 0011 张三 aaa 0022 李四 aaa 0013 王五 b

怎样实现这个SQL语句?
表A有列
id name code_name code
1 张三 aaa 001
1 张三 aaa 002
2 李四 aaa 001
3 王五 bbb 002

表B有列
name code
aaa 001
bbb 002

各位好,向大家请教个问题:有表A跟表B如上面所示,表B有001和002两个限制,则表A中每个人必须包含001和002两条数据,怎样用sql语句查询出表A中不满足条件的数据(即查询出表A中不同时包含001和002的人的姓名)? 上表查询出来应该是“李四”和“王五”,因为“张三”同时包含了001和002.

[解决办法]
select A.name
from A,B
where A.code=B.Code
group by A.name
having count(distinct A.code)=(select count(*) from B)
[解决办法]

SQL code
select A.namefrom A,Bwhere A.code=B.Codegroup by A.namehaving count(distinct A.code) < (select count(*) from B)
[解决办法]
SQL code
select A.namefrom A,Bwhere A.code=B.Codegroup by A.namehaving count(distinct A.code)=(select count(1) from B)
[解决办法]
SQL code
select A.namefrom Agroup by A.namehaving count(distinct A.code) < (select count(*) from B)
[解决办法]
select * from a group by name,code_name having count(*)<(select count(*) from b)
[解决办法]
select A.name
from A
join B on A.code=B.code 
group by A.name
having count(distinct A.code) < (select count(*) from B)
[解决办法]
select A.name
from A
where A.code in (select code from B)
group by A.name
having count(distinct A.code) <(select count(1) from B)
-------------------------
select distinct A1.name
from A A1
where exists(select * from B where B.code not in(select code from A A2 where A2.name=A1.name))
[解决办法]
SQL code
select name from A where (select count(name) from A where a.code='001' and b.code='002')<2 

热点排行