multiple variable length seaching on same column

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

    #1

    multiple variable length seaching on same column

    I am using

    field( substring( product_id, 1, 4 ),
    'IR56', '1k09', '7a72', '6f22' ) > 0

    in a where clause to select four different strings of same length.

    However I want to be able to search variable length strings, such as
    IR5671, 1k097, 7a72p123, 6f22qw. I will know the actual length of
    strings at run time. So please help me.

    Thanks,
    Yasaswi

  • Jonathan

    #2
    Re: multiple variable length seaching on same column

    yasaswi wrote:[color=blue]
    > I am using
    >
    > field( substring( product_id, 1, 4 ),
    > 'IR56', '1k09', '7a72', '6f22' ) > 0
    >
    > in a where clause to select four different strings of same length.
    >
    > However I want to be able to search variable length strings, such as
    > IR5671, 1k097, 7a72p123, 6f22qw. I will know the actual length of
    > strings at run time. So please help me.
    >
    > Thanks,
    > Yasaswi
    >[/color]
    Essentially you would like to do pattern-matching, MySQL uses the LIKE
    keyword for that purpose:

    http://dev.mysql.com/doc/mysql/en/pattern-matching.html

    For instance your query could look something like this:

    SELECT [fields] FROM [table] WHERE product_id LIKE '%IR56%' OR LIKE
    '%1k09%' OR LIKE '%7a72%' OR LIKE '%6f22%';

    The above query would list all records where product idea consists of
    one of the four search parameters anywhere in the field.

    Jonathan

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • yasaswi

      #3
      Re: multiple variable length seaching on same column

      Hi Jonathan:
      Thank you for your reply. How can I mix the "OR" operator with "AND"
      operator in the WHERE clause? I have 4 to 5 other conditions in the
      WHERE clause and the last one is the check for product_id ? Please let
      me know.
      Thanks,
      Yasaswi

      Comment

      • Jonathan

        #4
        Re: multiple variable length seaching on same column

        yasaswi wrote:[color=blue]
        > Hi Jonathan:
        > Thank you for your reply. How can I mix the "OR" operator with "AND"
        > operator in the WHERE clause? I have 4 to 5 other conditions in the
        > WHERE clause and the last one is the check for product_id ? Please let
        > me know.
        > Thanks,
        > Yasaswi
        >[/color]

        .... WHERE ( statement1 OR statement2 ) AND statement3

        would select all records that either match statement1 and statement3 or
        statement2 and statement3. Of course it works the other way around as well:

        .... WHERE ( statement1 AND statement2 ) OR statement3

        The latter would select all records that match statement1 and
        statement2, or match statement3 (and not statement1 and statement2).

        Hope this helps you out!

        Jonathan

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • yasaswi

          #5
          Re: multiple variable length seaching on same column

          Hi Jonathan:

          I am doing the following and getting a syntax error# 1064. Does the
          feature you mention above come with MySQL 5. I am using 4.1.12 standard
          version. Please inform me.

          where ( oi.product_id like 'IR56%' or like '1k09%' or like '7a72p%' or
          like '6f22%' )
          and os.order_id = oi.order_id
          and os.order_item_s eq_id = oi.order_item_s eq_id
          and os.party_id = 'company1'
          order by order_id asc, order_item_seq_ id asc;

          Thanks,
          Yasaswi

          Comment

          • Jonathan

            #6
            Re: multiple variable length seaching on same column

            yasaswi wrote:[color=blue]
            > Hi Jonathan:
            >
            > I am doing the following and getting a syntax error# 1064. Does the
            > feature you mention above come with MySQL 5. I am using 4.1.12 standard
            > version. Please inform me.
            >
            > where ( oi.product_id like 'IR56%' or like '1k09%' or like '7a72p%' or
            > like '6f22%' )
            > and os.order_id = oi.order_id
            > and os.order_item_s eq_id = oi.order_item_s eq_id
            > and os.party_id = 'company1'
            > order by order_id asc, order_item_seq_ id asc;
            >
            > Thanks,
            > Yasaswi
            >[/color]

            This option has been there since I know off, but you made the following
            mistake:

            You should specify the fieldname for every option (just like you do if
            different fields are concerned like the AND clauses you use). So it read
            something like this:

            where
            (
            oi.product_id like 'IR56%'
            or oi.product_id like '1k09%'
            or oi.product_id like '7a72p%'
            or oi.product_id like '6f22%'
            )
            and os.order_id = oi.order_id
            and os.order_item_s eq_id = oi.order_item_s eq_id
            and os.party_id = 'company1'
            order by
            order_id asc,
            order_item_seq_ id asc;

            (indentation is just for readability)

            Jonathan

            --
            A: Because it messes up the order in which people normally read text.
            Q: Why is it such a bad thing?
            A: Top-posting.
            Q: What is the most annoying thing on usenet and in e-mail?

            Comment

            • Felix Geerinckx

              #7
              Re: multiple variable length seaching on same column

              On 28/07/2005, Jonathan wrote:
              [color=blue]
              > This option has been there since I know off, but you made the
              > following mistake:[/color]

              It would be fair to say that not the OP but you made this mistake (in
              your first reply to this thread).

              --
              felix

              Comment

              • Jonathan

                #8
                Re: multiple variable length seaching on same column

                Felix Geerinckx wrote:[color=blue]
                > On 28/07/2005, Jonathan wrote:
                >
                > It would be fair to say that not the OP but you made this mistake (in
                > your first reply to this thread).
                >[/color]

                Sorry, my mistake! Thanks for pointing this out!

                Jonathan

                --
                A: Because it messes up the order in which people normally read text.
                Q: Why is it such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?

                Comment

                • tsq
                  New Member
                  • Aug 2005
                  • 1

                  #9
                  In your example

                  SELECT [fields] FROM [table] WHERE product_id LIKE '%IR56%' OR LIKE
                  '%1k09%' OR LIKE '%7a72%' OR LIKE '%6f22%'

                  would it be possible to use a $variable instead of a string of characters? :confused: I'm very new to PHP and I've looked everywhere for a solution to doing a partial match with a variable. Surely there's a way! Do I just need to change the syntax somehow? Should I be using something other than LIKE?

                  Thanks for any help!!

                  Comment

                  Working...