multiple inserts and deletes

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

    #1

    multiple inserts and deletes

    Hello all,

    I'm using PHP/mySQL couple :) to insert and delete some informations
    based on some criterium and I don't like to look at my code full of
    FOR and IF's.

    1)
    Example: In a list of ID's 1 4 5 7 8 9 20 34 56 is possible to send an
    UNIQUE command to mysql to delete the Id's 5, 7, 20, etc? Need I put a
    ugly query like "Delete ... WHERE id=5 AND id=7 AND id=20" ad
    infinitum?

    2) How can I check without loops if a given value exist in the table?
    Example: id's 1 3 5 7 8 must be inserted ONLY IF 3 or 5 doesn't exist.

    Finally, It's very hard to do some search for examples to know how to
    use the keyword "IN" , any hint please?

    Thank you all and I'm sorry for the long post.
  • Aggro

    #2
    Re: multiple inserts and deletes

    baruch wrote:
    [color=blue]
    > 1)
    > Example: In a list of ID's 1 4 5 7 8 9 20 34 56 is possible to send an
    > UNIQUE command to mysql to delete the Id's 5, 7, 20, etc? Need I put a
    > ugly query like "Delete ... WHERE id=5 AND id=7 AND id=20" ad
    > infinitum?[/color]

    delete from tablename where id in(5,7,20);
    [color=blue]
    > 2) How can I check without loops if a given value exist in the table?
    > Example: id's 1 3 5 7 8 must be inserted ONLY IF 3 or 5 doesn't exist.[/color]

    # I'm not sure if you can do this with only one query,
    # but with two queries
    select * from table where id in(3,5);

    # if previous didn't return any rows:
    insert into yourtable(id) values(1),(3),( 5),(7);

    Comment

    • Aggro

      #3
      Re: multiple inserts and deletes

      baruch wrote:
      [color=blue]
      > 1)
      > Example: In a list of ID's 1 4 5 7 8 9 20 34 56 is possible to send an
      > UNIQUE command to mysql to delete the Id's 5, 7, 20, etc? Need I put a
      > ugly query like "Delete ... WHERE id=5 AND id=7 AND id=20" ad
      > infinitum?[/color]

      delete from tablename where id in(5,7,20);
      [color=blue]
      > 2) How can I check without loops if a given value exist in the table?
      > Example: id's 1 3 5 7 8 must be inserted ONLY IF 3 or 5 doesn't exist.[/color]

      # I'm not sure if you can do this with only one query,
      # but with two queries
      select * from table where id in(3,5);

      # if previous didn't return any rows:
      insert into yourtable(id) values(1),(3),( 5),(7);

      Comment

      • Aggro

        #4
        Re: multiple inserts and deletes

        baruch wrote:
        [color=blue]
        > 1)
        > Example: In a list of ID's 1 4 5 7 8 9 20 34 56 is possible to send an
        > UNIQUE command to mysql to delete the Id's 5, 7, 20, etc? Need I put a
        > ugly query like "Delete ... WHERE id=5 AND id=7 AND id=20" ad
        > infinitum?[/color]

        delete from tablename where id in(5,7,20);
        [color=blue]
        > 2) How can I check without loops if a given value exist in the table?
        > Example: id's 1 3 5 7 8 must be inserted ONLY IF 3 or 5 doesn't exist.[/color]

        # I'm not sure if you can do this with only one query,
        # but with two queries
        select * from table where id in(3,5);

        # if previous didn't return any rows:
        insert into yourtable(id) values(1),(3),( 5),(7);

        Comment

        • baruch

          #5
          Re: multiple inserts and deletes

          > delete from tablename where id in(5,7,20);
          [..]

          Thank you very much!

          Comment

          • baruch

            #6
            Re: multiple inserts and deletes

            > delete from tablename where id in(5,7,20);
            [..]

            Thank you very much!

            Comment

            • baruch

              #7
              Re: multiple inserts and deletes

              > delete from tablename where id in(5,7,20);
              [..]

              Thank you very much!

              Comment

              Working...