查询表中手机号码为9位或者11位的(sj)请教,怎么在mysql中写查询语句了?select count(*) from table where sj='9' or sj='11';  ------------这里收集长度要用什么函数了??

解决方案 »

  1.   

    select count(*) from table where char_length(sj)='9' or char_length(sj)='11';
      

  2.   

    mysql中查某个字段的长度  
    length:   是计算字段的长度一个汉字是算三个字符,一个数字或字母算一个字符char_length:不管汉字还是数字或者是字母都算是一个字符
    mysql> select char_length('士大夫健康');
    +---------------------------+
    | char_length('士大夫健康') |
    +---------------------------+
    |                        10 |
    +---------------------------+
    1 row in set (0.00 sec)mysql> select length('士大夫健康');
    +----------------------+
    | length('士大夫健康') |
    +----------------------+
    |                   10 |
    +----------------------+
    在这里为什么char_length 和 length 的值是一样了??
      

  3.   

    mysql> create table tb_len_a(a varchar(20)) default character set  gbk;
    Query OK, 0 rows affected (0.01 sec)mysql> insert into tb_len_a values('我的')
        -> ;
    Query OK, 1 row affected (0.00 sec)mysql> select a,length(a) from tb_len_a;
    +------+-----------+
    | a    | length(a) |
    +------+-----------+
    | 我的 |         4 | 
    +------+-----------+
    1 row in set (0.01 sec)mysql> select a,char_length(a) from tb_len_a;
    +------+----------------+
    | a    | char_length(a) |
    +------+----------------+
    | 我的 |              2 | 
    +------+----------------+
    1 row in set (0.01 sec)mysql> 
      

  4.   

    oh,只有数据库本身存在才能查~~~ 字符集的问题。mysql> select length('我的'),char_length('我的');
    +----------------+---------------------+
    | length('我的') | char_length('我的') |
    +----------------+---------------------+
    |              4 |                   4 |
    +----------------+---------------------+
    1 row in set (0.00 sec)mysql> set names 'gb2312';
    Query OK, 0 rows affected (0.00 sec)mysql> select length('我的'),char_length('我的');
    +----------------+---------------------+
    | length('我的') | char_length('我的') |
    +----------------+---------------------+
    |              4 |                   2 |
    +----------------+---------------------+
    1 row in set (0.06 sec)mysql>