hwo to use REGEXP to find rows with any numbers in between?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poolboi
    New Member
    • Jan 2008
    • 170

    hwo to use REGEXP to find rows with any numbers in between?

    hey guys,

    i got a question on REGEXP in SQL

    if i have a row containing the info

    NAME : 12344455667678

    how can i use a SELECt with REGEXP
    [CODE=mysql]
    SELECT * FROM `mml log` WHERE Commands REGEXP
    [/CODE]

    such that it will search anything in the field with NAME : $input
    in which the $input is given by my users

    so say is $input is 234 or 444 or 5566
    it will still return me the row containing the info above?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You don't need REGEXP for that. Just a simple like with the % wildcard will do.
    If you insist on REGEXP then you can read about how to use it from here.

    Comment

    • poolboi
      New Member
      • Jan 2008
      • 170

      #3
      hm...i tried using wild card too

      the reason why it doesn't work is 'cos
      i want to make sure the number is attached to NAME

      so if i have 2 rows each containing;

      NAME :11223345565
      CLASS: 18373556879


      if i use use wild card with %556%
      it's gonna return both the rows
      but i only want it attached with NAME

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by poolboi
        hm...i tried using wild card too

        the reason why it doesn't work is 'cos
        i want to make sure the number is attached to NAME

        so if i have 2 rows each containing;

        NAME :11223345565
        CLASS: 18373556879


        if i use use wild card with %556%
        it's gonna return both the rows
        but i only want it attached with NAME
        I'm missing something here.
        Are NAME and CLASS two different columns?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by r035198x
          I'm missing something here.
          Are NAME and CLASS two different columns?
          Wait, I think I get you now.
          You can use concat. Concat 'NAME' with the wild carded input.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            If you have "NAME :11223345565" in a single field, you shouldn't!
            You should never have two pieces of data inside the same field.
            Check out this article.

            If that is how it is tho, you could simply do:
            Code:
            SELECT * FROM tbl WHERE col LIKE "NAME: %345%"
            The wild-cards can be anywhere inside a string, not just at the ends.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by Atli
              ..
              You should never have two pieces of data inside the same field.
              ...
              Amen .

              Comment

              • poolboi
                New Member
                • Jan 2008
                • 170

                #8
                hm.i get what u mean
                by right it should be under a column name

                actualli that field of mine is called commands
                commands that ppl use during a telnet session
                sometimes commands can be very long with number
                and i just wanna query say wildcard the number and get the results

                with regards to
                [CODE=mysql]

                NAME = %345%
                [/CODE]

                i've tried and doesn't worked too..

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by poolboi
                  hm.i get what u mean
                  by right it should be under a column name

                  actualli that field of mine is called commands
                  commands that ppl use during a telnet session
                  sometimes commands can be very long with number
                  and i just wanna query say wildcard the number and get the results

                  with regards to
                  [CODE=mysql]

                  NAME = %345%
                  [/CODE]

                  i've tried and doesn't worked too..
                  Wildcards are used with the LIKE operator not the = operator.
                  Also I think you want to do
                  Code:
                   LIKE 'NAME:%345%'

                  Comment

                  Working...