oracle查询语句的问题。? 400 报错
select * from news where content like '%黑%' and type='null';
select * from news where content like '%黑%' and type=null
请问为什么查不出来,显示某位选定行?(里面有那个黑字)
我设置joketype为null是想让type不作为查找条件,就是type为人任意值均可,现在想想,应该是那个type=null这块儿出错了,请问应该怎么改呢?
我想达成的目的是:type可以被我指定为某一类型,也可不做约束。
DAO层里String sql="select * from news where content like '?' and type=?";
请问这个type如果可以为任意值的话要怎么设置值?
谢谢!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
这样?
select * from news where content like '%黑%' and (type=&t or &t is null);
我是想要把一个值通过action传到DAO层里,来替代sql语句中type=?中的问号的。这不过,我想限制成type=任意值或者也可以传具体某个值给type。我之前可能没表述清楚,请问这个该如何写?
PS:你刚才教我的语句在oracle里也没运行成功,输入了两遍t值,之后提示我第二个t值无效标识符。
type栏位是字符型的对吧?那在oracle里这么写
select * from news where content like '%黑%' and (type='&t' or '&t' is null);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我在JAVA里试了下,可以这么写。
String p = null;
if (false) p = "1";
String sql = "select 1 from dual where '1' = ? or ? is null";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, p) ;
pstmt.setString(2, p);
应该先判断要不要添加and type=?的条件吧?
type等于什么都不行啊。
######这样?
select * from news where content like '%黑%' and (type=&t or &t is null);
######type is null######这样?
select * from news where content like '%黑%' and (type=&t or &t is null);
我是想要把一个值通过action传到DAO层里,来替代sql语句中type=?中的问号的。这不过,我想限制成type=任意值或者也可以传具体某个值给type。我之前可能没表述清楚,请问这个该如何写?
PS:你刚才教我的语句在oracle里也没运行成功,输入了两遍t值,之后提示我第二个t值无效标识符。
应该先判断要不要添加and type=?的条件吧?
type等于什么都不行啊。
是写两个sql语句吗?好像可行啊,这方法,我先试试啊。谢谢。
不过请问有没有一个sql就能判断这种情况的语句呢?
- -! 这东西一楼就已经解答你鸟!
先在dao里面判断type的传入值是否为空,为空则
sql = "select * from news where content like '%黑%'"
否则
sql = "select * from news where content like '%黑%' and type = ?"
注意为空的时候不要设置参数就好了DAO:
--若为任意,把传入的值设置为% --否则为实际匹配 select * from news where content like '?' and type like '?'
SQL实际显示:
任意值: select * from news where content like '%黑%' and type LIKE '%' 部分匹配1: select * from news where content like '%黑%' and type LIKE 'ABC%' 部分匹配2: select * from news where content like '%黑%' and type LIKE 'ABC%DEF' 等值匹配: select * from news where content like '%黑%' and type LIKE 'ABCDEF'
######
不想用type为查询条件,不用不就行了
select * from news where content like '%黑%' ;
如果你想type在查询的时候再人为输入的话,
select * from news where content like '%黑%' and type=&i;这样在查询运行的时候输入type的值就行了