开发者社区 问答 正文

oracle查询语句的问题。? 400 报错

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如果可以为任意值的话要怎么设置值?

谢谢!

 

 

展开
收起
爱吃鱼的程序员 2020-06-03 16:15:36 462 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    引用来自“亡灵S”的答案

    引用来自“no502zhang”的答案

    这样?

    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######

    引用来自“喵了个咪”的答案

    type is null
    不行,还是未选定行。
    ######

    引用来自“no502zhang”的答案

    这样?

    select * from news where content like '%黑%' and (type=&t or &t is null);

    我是想要把一个值通过action传到DAO层里,来替代sql语句中type=?中的问号的。这不过,我想限制成type=任意值或者也可以传具体某个值给type。我之前可能没表述清楚,请问这个该如何写?

    PS:你刚才教我的语句在oracle里也没运行成功,输入了两遍t值,之后提示我第二个t值无效标识符。

    ######

    引用来自“hulubo”的答案

    应该先判断要不要添加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的值就行了

    2020-06-03 17:32:50
    赞同 展开评论