在MySQL中不等于(!=或)不起作用 [英] Not Equals(!= or ) in mysql not working

查看:186
本文介绍了在MySQL中不等于(!=或)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我的查询是这样的

SELECT DISTINCT (s.sid),class,division,fatmobile,rollno,stdname,regno FROM termdetails t JOIN studentdetails s WHERE t.sid != s.sid AND s.class = ''" + ClassWiseDrop1.SelectedValue + "'' AND s.division = ''" + ClassWiseDrop2.SelectedValue + "''


此查询返回所有详细信息(其中,表termdetails中也包含sid)也会出现,因此此处not equals运算符不起作用.我该如何解决我的问题
谁能帮助我.
在此先感谢

解决方案

您误解了联接的工作方式.实际上,这将产生接近外部联接的内容,只丢失两者匹配的行.我认为您想要对termdetails进行简单选择,例如
select * from termdetails where not sid in (select sid from studentdetails)
(在5之前的版本中,您必须自己构造列表中的对象.请注意,我尚未对此进行测试,因此可能出了点问题.)

关于您实际上到底想做什么,您的问题尚不完全清楚.


SELECT DISTINCT (s.sid),class,division,fatmobile,rollno,stdname,regno FROM termdetails t WHERE t.sid NOT IN ( SELECT s.sid from studentdetails s WHERE (s.class = ''" + ClassWiseDrop1.SelectedValue + "'' AND s.division = ''" + ClassWiseDrop2.SelectedValue + "''))



您为什么不尝试使用上面的代码.我认为这可能会有所帮助.或者,如果您遇到其他问题,请尝试使用UNION运算符.

禁止使用 [禁止使用2 [ 选择 ID FROM TableName (名称<> ' 香蕉')


Hi all
my query goes like this

SELECT DISTINCT (s.sid),class,division,fatmobile,rollno,stdname,regno FROM termdetails t JOIN studentdetails s WHERE t.sid != s.sid AND s.class = ''" + ClassWiseDrop1.SelectedValue + "'' AND s.division = ''" + ClassWiseDrop2.SelectedValue + "''


this query returns all the details (in which sid is present in table termdetails also coming) so here not equals operator not working. how can i resolve my problem
can any one please help me.
thanks in advance

解决方案

You are misunderstanding how a join works. That will actually produce something close to an outer join, only missing the rows where the two match. I think what you want to do is a simple select on termdetails, something like
select * from termdetails where not sid in (select sid from studentdetails)
(In versions before 5 you have to construct the in-list yourself. And note that I haven''t tested this so something might be wrong.)

Your question is not totally clear about what you''re actually trying to do though.


SELECT DISTINCT (s.sid),class,division,fatmobile,rollno,stdname,regno FROM termdetails t WHERE t.sid NOT IN ( SELECT s.sid from studentdetails s WHERE (s.class = ''" + ClassWiseDrop1.SelectedValue + "'' AND s.division = ''" + ClassWiseDrop2.SelectedValue + "''))



Why dont you try making use of the above code. I think this might help. Or try using the UNION operator if you come across any more problems.

NOT IN Usage[^]
NOT IN Usage 2[^]

Here is an example of how NOT IN can be used.


The != (not is) operator in SQL is <>

SELECT ID FROM TableName WHERE (Name <> 'banana')


这篇关于在MySQL中不等于(!=或)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆