
oracle SQL查询中,如何在where中用条件语句,判断不同情况,追加不同的And条件? 30
表中有一个username字段(varchar),存储着几个用户名(tom,jack,张三),但也可能为空。如何判断一个用户名李四,当username字段不为空时,追加一...
表中有一个username字段(varchar),存储着几个用户名(tom,jack,张三),但也可能为空。如何判断一个用户名李四,当username字段不为空时,追加一个and条件“and instr(username, '李四') > 0“,来判断username中是否有李四这个字符串。当username不为空时,不追加任何条件。select * from table where 1=1 and (username is not null and instr(username , '李四') > 0) or (username is null)。我只写成这样,求改正?
展开
展开全部
1、先创建一个简单的数据表。
2、插入数据,顺便复习插入数据了,最好多插几个,查询的时候更明显。
3、select后面的字段是等下查询结果所显示的,where就是限制的条件,根据where查询到对应的列。
4、如果字段里面有数字的话也是可以根据数字大小进行查询的。
5、加入and语句可以同时限制两个条件来查询。
6、用%来查询,百分号在字母前表示以什么字母开头的,这里就是以名字为d开头来查询数据,同理把%放在后面就是以d结尾的名字。
展开全部
你是不是描述的有错误?怎么两次都是username不为空?
估计可以有好几个:
1、可以用union all
select * from table where 1=1 and (username is not null and instr(username , '李四') > 0)
union all
select * from table where username is null
2、你写的这个好像or and的逻辑有问题,可以改改
select * from table where 1=1
and ((username is not null and instr(username , '李四') > 0) or (username is null))
理解错误的话请纠正
更多追问追答
追问
需求:当username字段中为空时,就是“select * from table;”当username不为空的时候,判断“李四”在username中吗,就是“select * from table where 1=1 and instr(username , '李四') > 0;”这两种情况是互斥的,用一个SQL能把结果查出来吗?
追答
但是字段是一列值,有空或非空。
你说的为username空或非空是指传参吗?
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询