SQL statement help, 5 minutes of your help. paying 5 bucks

Discussion in 'MySQL' started by -jay-, May 30, 2009.

  1. #1
    paying 5 bucks for 5 a few minutes of your time if you know sql good.

    I need a statement. i will give you the scenario.

    I have a database.
    There is a table called Table_1
    In this table there is a column called Active_inactive which will have either an I or A in it for each record.

    I have 50 documents I want to change this record for, from I to A.
    I have to query the db with a couple of variables...
    By project ID (the database has thousands of projects each with unique id called Project_id)
    There is also another column called 'revisions'
    This can either have an A,B, or C.
    I need only the docs that have A.

    So....

    Find all 50 documents (with specific names, lets just say numbered 1-50) in specific project with A in the revisions column and I in the Active_Inactive column, then replace all the I's with A's in the revisions column.


    anyone ?

    thanks
    jay
     
    -jay-, May 30, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Is this MySQL or MSSQL?

    Something like this should work:

    UPDATE Table_1
    SET Active_Inactive = 'A'
    WHERE Project_id = 'WHATEVER ID YOU WANT'
    AND revisions = 'A'
    AND Active_Inactive = 'I';

    Not sure about the project id part. DO you need to update from a specific list, or do they all have the same Project_id?
     
    jestep, May 30, 2009 IP
  3. -jay-

    -jay- Well-Known Member

    Messages:
    2,311
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    160
    #3
    all different project id's

    but what about the document names ?
    its mssql
     
    -jay-, May 31, 2009 IP
  4. -jay-

    -jay- Well-Known Member

    Messages:
    2,311
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    160
    #4
    anyone ? jestep ?
     
    -jay-, May 31, 2009 IP
  5. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #5
    Agree the above should update the active column for ALL documents where the document is currently INACTIVE and revision is A. You stated that you have 50 documents that need to be updated; can I assume there are other documents that meet the above criteria that you do not want to update? If so, you must change the expression in the WHERE clause.

    UPDATE Table_1
    SET Active_Inactive = 'A'
    WHERE revisions = 'A'
    AND Active_Inactive = 'I'
    AND DocumentId IN ([insert document ids here]);

    OR

    UPDATE Table_1
    SET Active_Inactive = 'A'
    WHERE revisions = 'A'
    AND Active_Inactive = 'I'
    AND DocumentId IN (SELECT DocumentId FROM Table_1 WHERE [insert expression here]);
     
    Social.Network, May 31, 2009 IP
  6. -jay-

    -jay- Well-Known Member

    Messages:
    2,311
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    160
    #6
    why the () ?
    wouldnt it be like this, the last line ?
    AND Document_column = '1.jpg,2.jpg,3.jpg';

    is a space after each comma ?

    each doc updated is located in here
    WHERE Project_id = 'WHATEVER ID YOU WANT'

    so only docs in that project will be updated so no need for worrying about other docs
     
    -jay-, May 31, 2009 IP
  7. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #7
    When using the IN operator, you are specifying a list of values.

    SELECT column_name(s)
    FROM table_name
    WHERE column_name IN (value1,value2,...)

    The syntax is IN ( [insert a comma-delimited list of values to match] ). The way you wrote it will not give you the results you want, because it is trying to match where Document_column is equal to '1.jpg,2.jpg,3.jpg'; I hope that makes sense.
     
    Social.Network, May 31, 2009 IP
  8. -jay-

    -jay- Well-Known Member

    Messages:
    2,311
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    160
    #8
    ok so it should be

    AND Document_column IN ([1.jpg,2.jpg,3.jpg]);

    whole statement will be....

    UPDATE Table_1
    SET Active_Inactive_column = 'A'
    WHERE Project_id-column = '502998'
    AND revisions_column = 'A'
    AND Active_Inactive_column = 'I'
    AND Document_column IN '([1.jpg,2.jpg,3.jpg]);


    I just read it should be like this...

    AND Document_column IN ('1.jpg','2.jpg','3.jpg');
     
    -jay-, May 31, 2009 IP
  9. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #9
    Drop the "[ ]" brackets from the IN operator list. I was using them to denote a list of values. Also, if the values you are matching are text be sure to enclose them in quotes. For example '1.jpg', '2.jpg', 3.jpg'. If you have 50 documents for each project, this is going to be a challenge. I would consider using a sub-query to provide the list of values for the IN operator.

    AND Document_column IN (SELECT Document_column WHERE expression).

    Also, if you use a sub-query you might be able to drop the Project_id column from the above UPDATE statement.
     
    Social.Network, May 31, 2009 IP
  10. -jay-

    -jay- Well-Known Member

    Messages:
    2,311
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    160
    #10
    ok, i think i am set with this

    UPDATE Table_1
    SET Active_Inactive_column = 'A'
    WHERE Project_id-column = '502998'
    AND revisions_column = 'A'
    AND Active_Inactive_column = 'I'
    AND Document_column IN ('1.jpg','2.jpg','3.jpg');

    the docs are images so it will be just like that, its 50 but i dont mind typing it all in.

    thanks, shoot me a pm with your paypal id, ill send the 5 bucks. thanks.
    by the way, any way to test this for syntax errors ?
     
    -jay-, May 31, 2009 IP