日期:2014-05-19  浏览次数:20521 次

求高手帮我修改一条SQL语句
现有2个表。
gw_enter(记录向该会员添加一个物品)
ID       thing
1           小作业本
1           黑色铅笔
1           白色文具盒
2           黑色文具盒

gw_subject(该表存放了物品的列表)
ID         thing           subject(控制物品类别的)
1           小作业本       1
2           大作业本       1
3           作文本           1
4           白色铅笔       2
5           黑色铅笔       2
6           白色文具盒   3
7           黑色文具盒   3
现在我想读出这样的值:
前提:每一个会员同一类别的物品只能分配一样,
现在我要取这个会员当前状态下,
还能够给他分配的所有的东西。
如:会员2。
现在他只有一个黑色文具盒。
其文具盒的subject是3
那么我需要读出来的值就应该是gw_subject中间subject!=3的所有的值。
我这样写语句,但是却什么值都读不出来。
求高手指点一下。
select   *   from     gw_subject   where   subject   !=(select   subject   from   gw_subject   where   thing=(select   thing   from   gw_enter   where   id= ' "   +   TextBox1.Text   +   " '))


------解决方案--------------------
try


select * from gw_subject where subject Not In (select A.subject from gw_subject A Inner Join gw_enter B On A.thing= B.thing where B.id= ' " + TextBox1.Text + " ')
------解决方案--------------------
裡面的子查詢可以用關聯來寫。

另外,!=改為Not In試下
------解决方案--------------------
select * from gw_subject a
where not exists (
select 1 from gw_enter b,gw_subject c
where b.thing=c.thing
and c.subject=a.subject
and b.id= ' " + TextBox1.Text + " '
)


------解决方案--------------------
select *
from gw_subject
where subject not in (select subject from gw_subject as a inner join gw_enter as b on a.thing=b.thing where b.id= ' " + TextBox1.Text + " ' group by subject)