我在navicat中如题的sql语句查询时却得到一个0,但是我的数据表里面有多条username是ss的记录,如果用select count(*) from user where username='s'这条语句返回的就是10,为什么得到的是0,有人懂吗

解决方案 »

  1.   

    select count(*) from user where username='%ss%' 
    ----可能的原因是:等号 (=)问题 。
    通配符的意思是“像”,不精确,应该用 like,不能用表示严格的等号。试试这个:select count(*) from user where username LIKE '%ss%' 
      

  2.   

    你好是这样的,那个问题我已经知道是等号的问题,但是现在我碰到一个类似的问题,我在数据表用select Count(*) from user where user.sex=1 and user.username like '%小明%'这个查询语句查询得到的值是3,然后我用mybatis做一个测试得到的却是一个null;
    这是<select id="findCount" parameterType="User_UserCustom_vo" resultType="userCustom">
    select Count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>的查询语句设置注入的值是1和小明,为什么我在数据表查询可以得到值,而在mybatis查询的到却是null
    这是测试的方法
    @Test
    public void testFindCount() throws Exception
    {
    SqlSession session =sqlSessionFactory.openSession();
    UserMapper userMapper  =session.getMapper(UserMapper.class);
    UserCustom userCustom = new UserCustom();
    userCustom.setSex("1");
    userCustom.setUsername("小明");
    User_UserCustom_vo user_UserCustom_vo = new User_UserCustom_vo();
    user_UserCustom_vo.setUserCustom(userCustom);
    int i =userMapper.findCount(user_UserCustom_vo);
    System.err.println(i);
    }
    这是User_UserCustom_vo 类
    public class User_UserCustom_vo {
    private List<Integer> ids;


    public List<Integer> getIds() {
    return ids;
    } public void setIds(List<Integer> ids) {
    this.ids = ids;
    } private UserCustom userCustom; public UserCustom getUserCustom() {
    return userCustom;
    } public void setUserCustom(UserCustom userCustom) {
    this.userCustom = userCustom;
    }

    }
      

  3.   

    你好是这样的,那个问题我已经知道是等号的问题,但是现在我碰到一个类似的问题,我在数据表用select Count(*) from user where user.sex=1 and user.username like '%小明%'这个查询语句查询得到的值是3,然后我用mybatis做一个测试得到的却是一个null;
    这是<select id="findCount" parameterType="User_UserCustom_vo" resultType="userCustom">
    select Count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>的查询语句设置注入的值是1和小明,为什么我在数据表查询可以得到值,而在mybatis查询的到却是null
    这是测试的方法
    @Test
    public void testFindCount() throws Exception
    {
    SqlSession session =sqlSessionFactory.openSession();
    UserMapper userMapper  =session.getMapper(UserMapper.class);
    UserCustom userCustom = new UserCustom();
    userCustom.setSex("1");
    userCustom.setUsername("小明");
    User_UserCustom_vo user_UserCustom_vo = new User_UserCustom_vo();
    user_UserCustom_vo.setUserCustom(userCustom);
    int i =userMapper.findCount(user_UserCustom_vo);
    System.err.println(i);
    }
    这是User_UserCustom_vo 类
    public class User_UserCustom_vo {
    private List<Integer> ids;


    public List<Integer> getIds() {
    return ids;
    } public void setIds(List<Integer> ids) {
    this.ids = ids;
    } private UserCustom userCustom; public UserCustom getUserCustom() {
    return userCustom;
    } public void setUserCustom(UserCustom userCustom) {
    this.userCustom = userCustom;
    }

    }
    这是控制台的信息:DEBUG [main] - Created connection 1891254926.
    DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@70ba428e]
    DEBUG [main] - ==>  Preparing: select Count(*) from user where user.sex=? and user.username like '%小明%' 
    DEBUG [main] - ==> Parameters: 1(String)
    DEBUG [main] - <==      Total: 1
    null
      

  4.   

    LIKE 
      

  5.   

    like 
    instr  locate
      

  6.   

    把等号换成like
      

  7.   

    select Count(*) from user where user.sex=#{userCustom.sex} 
    这里的#号什么意思,搞错了吧 $吧,如果是这个问题,你要仔细了
     like '%${userCustom.username}%' 
      

  8.   

    =号换like...
      

  9.   


    # 号是预编译的哦
    $是拼SQL的。
      

  10.   


    # 号是预编译的哦
    $是拼SQL的。

    没搞过mybatis ,能输出mybatis执行的语句吗,输出执行的语句贴出出来看
      

  11.   

    COUNT(*) 查询的是记录数,当无结果返回时,默认返回0,如果直接把 count(*) 换成 * ,当无结果时,则返回空;
    条件里面的等于号后面 单引号里面的字符串,系统识别为某一个值,user 表不存在,所以返回0条,等于号改为 like 代表模糊查询;