例如表结构是:
uid tuijian shenhe
1 1 1
2 1 0
3 2 0
uid就是用户名,tuijian表示这个用户的推荐用户,shenhe 表示是否通过审核.现在想用一条查询语句来查询 没有通过审核(数字为0)的人的用户名和他的推荐人的审核状况(用tjshenhe这个别名表示).上面的应该是
uid tjshenhe
2 1
3 1
uid tuijian shenhe
1 1 1
2 1 0
3 2 0
uid就是用户名,tuijian表示这个用户的推荐用户,shenhe 表示是否通过审核.现在想用一条查询语句来查询 没有通过审核(数字为0)的人的用户名和他的推荐人的审核状况(用tjshenhe这个别名表示).上面的应该是
uid tjshenhe
2 1
3 1
select uid,count(*) as tjshenhe from 表 where shenhe=0 group by uid
应该是选择没有通过审核的人的用户名和没有通过审核的人的总数量.
我的tjshenhe表示的是这个用户的推荐人的审核情况.
比如3这个用户的推荐人是2.2的shenhe是0.所以他的tjshenhe是0.
uid tuijian shenhe
1 1 1
2 1 0
3 2 0uid tjshenhe
2 1
3 0
是不是这样?
不是那样的。
uid in (select tuijian from 表名 where shenhe=0)
uid和tuijian不是包含关系。uid不能嵌套在tuijian中的。
A.uid,
B.shenhe As tjshenhe
From
表 A
Inner Join
表 B
On A.tuijian = B.uid
Where A.shenhe = 0
(uid Int,
tuijian Int,
shenhe Bit)
Insert 表 Select 1, 1, 1
Union All Select 2, 1, 0
Union All Select 3, 2, 0
GO
Select
A.uid,
B.shenhe As tjshenhe
From
表 A
Inner Join
表 B
On A.tuijian = B.uid
Where A.shenhe = 0
GO
Drop Table 表
--Result
/*
uid tjshenhe
2 1
3 0
*/