有两个表:表a 和表b,有共同的一列:bh,表a的bh列中有表b中bh列中没有的数据,现在我要挑出表a的列bh中有而表b中bh的列没有的的所有的数据所在行行。语言如下:
select *
from a
where bh not in (select bh from b);
但结果连一行数据都没有,为什么啊
select *
from a
where bh not in (select bh from b);
但结果连一行数据都没有,为什么啊
解决方案 »
- 因为该列没有包含在聚合函数或 GROUP BY 子句中。 啥意思??
- 求个查询
- 使用聚合函数的sql语句一定要as 新列名me ?
- 求语句,删除表中某列的所有值
- 请教个问题 各位达人请进啊
- 如何将sql2000的数据库转换成sql2005,并可以正常使用
- 为什么我的SQLFetchScroll(hstmt, SQL_FETCH_RELATIVE, SeqNum)老是不成功呢?返回总是-1
- 视图是否会更新?明白即揭贴!
- 不用存储过程如何读出数据库中的图片?
- 在存储过程中,要是想要使用CHARINDEX来分割字符串要怎样做?SUBSTRING又要怎样使用?
- 这样的数据库结构,如何排列,同时排列第一条记录和最后一条记录
- 排序问题------急!!!
select select * from a where not exists(select 1 from b where a.bh=b.bh)
如:
a表
--
a
d
cb 表
------------
a
a
c
c
c1在这没意思,你明白exists 就可以了~只返回一个真假,1你想换什么都可以,1可以说是没意义的
declare @t table(id int,name varchar(10))
insert @t
select 1,'aa'
union all select 3,'cc'
union all select 4,'c'
union all select 5,'dd'
union all select 6,'e'
declare @b table(id int,name varchar(10))
insert @b
select 1,'a'
union all select 3,'c'
union all select 4,'ce'
union all select 7,'ddd'select * from @t
where id not in (select id from @b)
你再检查一下其它的原因吧
from a
where bh not in (select bh from b);
----------
沒有分號,這句話是沒有錯的,只b中有a 的記錄是不會顯示出來的orselect * from a where not exists(select 1 from b where a.bh=b.bh)
要快一點
注意...
select * from a where not exists(select 1 from b where a.bh=b.bh)
和
select * from a where not in(select 1 from b where a.bh=b.bh)
意思不一样!!!