php mysql select substring

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jossinet

    #1

    php mysql select substring

    Bonjour,
    j'ai une table : matable
    dans laquelle j'ai un champs : codepostal

    Ce champs code postal est rempli d'enregistremen ts qui ont des valeurs
    de 5 chiffres.
    Je souhaite créer un bouton qui va sélectionner les enregistrements
    dont la valeur du code postal commence par les 2 premiers chiffres
    (ici par le chiffre 76).

    J'ai écrit le code suivant pour la sélection...et ca ne fonctionne pas
    !

    $query_rs_epart ners = "SELECT * FROM matable WHERE
    SUBSTRING(codep ostal, 2)=76";

    Donc dans ce cas, je souhaite afficher les enregistrements dont le
    code postal commence par 76

    Qu'y a il de faux ?

    D'avance MERCI
  • Aggro

    #2
    Re: php mysql select substring

    jossinet wrote:
    [color=blue]
    > $query_rs_epart ners = "SELECT * FROM matable WHERE
    > SUBSTRING(codep ostal, 2)=76";[/color]

    I have no idea what you are asking, because you didn't use English, but
    I try to answer anyway. Here is an example how substring is used:

    mysql> SELECT SUBSTRING('Quad ratically',5);
    -> 'ratically'

    It will return a string, not a number. If you have a string like '1276'
    in your codepostal column and you would like to find that column in your
    select, you could perhaps do something like:

    select * from matable where substring(codep ostal,2)='76';

    Note that '76' is a string and 76 is a number.

    More info about string functions:
    http://www.mysql.com/doc/en/String_functions.html

    Comment

    • Aggro

      #3
      Re: php mysql select substring

      jossinet wrote:
      [color=blue]
      > $query_rs_epart ners = "SELECT * FROM matable WHERE
      > SUBSTRING(codep ostal, 2)=76";[/color]

      I have no idea what you are asking, because you didn't use English, but
      I try to answer anyway. Here is an example how substring is used:

      mysql> SELECT SUBSTRING('Quad ratically',5);
      -> 'ratically'

      It will return a string, not a number. If you have a string like '1276'
      in your codepostal column and you would like to find that column in your
      select, you could perhaps do something like:

      select * from matable where substring(codep ostal,2)='76';

      Note that '76' is a string and 76 is a number.

      More info about string functions:
      http://www.mysql.com/doc/en/String_functions.html

      Comment

      • Aggro

        #4
        Re: php mysql select substring

        jossinet wrote:
        [color=blue]
        > $query_rs_epart ners = "SELECT * FROM matable WHERE
        > SUBSTRING(codep ostal, 2)=76";[/color]

        I have no idea what you are asking, because you didn't use English, but
        I try to answer anyway. Here is an example how substring is used:

        mysql> SELECT SUBSTRING('Quad ratically',5);
        -> 'ratically'

        It will return a string, not a number. If you have a string like '1276'
        in your codepostal column and you would like to find that column in your
        select, you could perhaps do something like:

        select * from matable where substring(codep ostal,2)='76';

        Note that '76' is a string and 76 is a number.

        More info about string functions:
        http://www.mysql.com/doc/en/String_functions.html

        Comment

        • jossinet

          #5
          Re: php mysql select substring

          Aggro <spammerdream@y ahoo.com> wrote in message news:<rvo1c.243 $hq6.123@read3. inet.fi>...[color=blue]
          > jossinet wrote:
          >[color=green]
          > > $query_rs_epart ners = "SELECT * FROM matable WHERE
          > > SUBSTRING(codep ostal, 2)=76";[/color]
          >
          > I have no idea what you are asking, because you didn't use English, but
          > I try to answer anyway. Here is an example how substring is used:
          >
          > mysql> SELECT SUBSTRING('Quad ratically',5);
          > -> 'ratically'
          >
          > It will return a string, not a number. If you have a string like '1276'
          > in your codepostal column and you would like to find that column in your
          > select, you could perhaps do something like:
          >
          > select * from matable where substring(codep ostal,2)='76';
          >
          > Note that '76' is a string and 76 is a number.
          >
          > More info about string functions:
          > http://www.mysql.com/doc/en/String_functions.html[/color]


          Thank you for the answer...but it still doesn't work.
          I have a table : matable
          A column : codepostal

          The codepostal is a number with 5 numbers (it's a zip code like 55250)
          I want to select only the two first numbers of the codepostal that correspond to 76.
          For instance if the codepostal is 76890, it should be selected.
          If the codepostal is 47620, it should not be selected.
          So, I tried to write :
          select * from matable where substring(codep ostal,0,2)='76' ;
          But nothing is selected....and I have some codepostal that start with 76 !
          Thank You

          Comment

          • jossinet

            #6
            Re: php mysql select substring

            Aggro <spammerdream@y ahoo.com> wrote in message news:<rvo1c.243 $hq6.123@read3. inet.fi>...[color=blue]
            > jossinet wrote:
            >[color=green]
            > > $query_rs_epart ners = "SELECT * FROM matable WHERE
            > > SUBSTRING(codep ostal, 2)=76";[/color]
            >
            > I have no idea what you are asking, because you didn't use English, but
            > I try to answer anyway. Here is an example how substring is used:
            >
            > mysql> SELECT SUBSTRING('Quad ratically',5);
            > -> 'ratically'
            >
            > It will return a string, not a number. If you have a string like '1276'
            > in your codepostal column and you would like to find that column in your
            > select, you could perhaps do something like:
            >
            > select * from matable where substring(codep ostal,2)='76';
            >
            > Note that '76' is a string and 76 is a number.
            >
            > More info about string functions:
            > http://www.mysql.com/doc/en/String_functions.html[/color]


            Thank you for the answer...but it still doesn't work.
            I have a table : matable
            A column : codepostal

            The codepostal is a number with 5 numbers (it's a zip code like 55250)
            I want to select only the two first numbers of the codepostal that correspond to 76.
            For instance if the codepostal is 76890, it should be selected.
            If the codepostal is 47620, it should not be selected.
            So, I tried to write :
            select * from matable where substring(codep ostal,0,2)='76' ;
            But nothing is selected....and I have some codepostal that start with 76 !
            Thank You

            Comment

            • jossinet

              #7
              Re: php mysql select substring

              Aggro <spammerdream@y ahoo.com> wrote in message news:<rvo1c.243 $hq6.123@read3. inet.fi>...[color=blue]
              > jossinet wrote:
              >[color=green]
              > > $query_rs_epart ners = "SELECT * FROM matable WHERE
              > > SUBSTRING(codep ostal, 2)=76";[/color]
              >
              > I have no idea what you are asking, because you didn't use English, but
              > I try to answer anyway. Here is an example how substring is used:
              >
              > mysql> SELECT SUBSTRING('Quad ratically',5);
              > -> 'ratically'
              >
              > It will return a string, not a number. If you have a string like '1276'
              > in your codepostal column and you would like to find that column in your
              > select, you could perhaps do something like:
              >
              > select * from matable where substring(codep ostal,2)='76';
              >
              > Note that '76' is a string and 76 is a number.
              >
              > More info about string functions:
              > http://www.mysql.com/doc/en/String_functions.html[/color]


              Thank you for the answer...but it still doesn't work.
              I have a table : matable
              A column : codepostal

              The codepostal is a number with 5 numbers (it's a zip code like 55250)
              I want to select only the two first numbers of the codepostal that correspond to 76.
              For instance if the codepostal is 76890, it should be selected.
              If the codepostal is 47620, it should not be selected.
              So, I tried to write :
              select * from matable where substring(codep ostal,0,2)='76' ;
              But nothing is selected....and I have some codepostal that start with 76 !
              Thank You

              Comment

              • Aggro

                #8
                Re: php mysql select substring

                jossinet wrote:
                [color=blue]
                > So, I tried to write :
                > select * from matable where substring(codep ostal,0,2)='76' ;
                > But nothing is selected....and I have some codepostal that start with 76 ![/color]

                Next time you encounter a problem like this, try instead to find out
                what it is that you are actually selecting. i.e.:
                select substring(codep ostal,0,2) as sub from matable;

                As you can see, it returns empty strings, the reason for that would been
                found from the manual, because string index doesn't start from 0, it
                starts from 1.

                So what you need is:
                select * from matable where substring(codep ostal,1,2)='76' ;

                Comment

                • Aggro

                  #9
                  Re: php mysql select substring

                  jossinet wrote:
                  [color=blue]
                  > So, I tried to write :
                  > select * from matable where substring(codep ostal,0,2)='76' ;
                  > But nothing is selected....and I have some codepostal that start with 76 ![/color]

                  Next time you encounter a problem like this, try instead to find out
                  what it is that you are actually selecting. i.e.:
                  select substring(codep ostal,0,2) as sub from matable;

                  As you can see, it returns empty strings, the reason for that would been
                  found from the manual, because string index doesn't start from 0, it
                  starts from 1.

                  So what you need is:
                  select * from matable where substring(codep ostal,1,2)='76' ;

                  Comment

                  • Aggro

                    #10
                    Re: php mysql select substring

                    jossinet wrote:
                    [color=blue]
                    > So, I tried to write :
                    > select * from matable where substring(codep ostal,0,2)='76' ;
                    > But nothing is selected....and I have some codepostal that start with 76 ![/color]

                    Next time you encounter a problem like this, try instead to find out
                    what it is that you are actually selecting. i.e.:
                    select substring(codep ostal,0,2) as sub from matable;

                    As you can see, it returns empty strings, the reason for that would been
                    found from the manual, because string index doesn't start from 0, it
                    starts from 1.

                    So what you need is:
                    select * from matable where substring(codep ostal,1,2)='76' ;

                    Comment

                    Working...