Is it possible to combine update and select statements in PHP?

January 23, 2010 - 9:49 am

I have a table containing 2 columns. The first column is already filled. I want to update 2nd column of all records in this table using the values available in ANOTHER table based on the value of the first column of first table. Something like - UPDATE TABLE1 SET COLUMN2OFFIRSTTABLE = SELECT COLUMN2OF2NDTABLE FROM TABLE2 WHERE COLUMN1OFFIRSTTABLE=COLUMN1OF2NDTABLE; or any other command to achieve the same effect??

I don’t think it’s possible to do it the way you’re asking. But why don’t you just separate the queries? It will do the same thing either way.

2 Responses to “Is it possible to combine update and select statements in PHP?”

  1. Err Says:

    I don’t think it’s possible to do it the way you’re asking. But why don’t you just separate the queries? It will do the same thing either way.
    References :

  2. Paul Says:

    You can use a JOIN statement:

    UPDATE table1 SET COLUMN2OFFIRSTTABLE = COLUMN2OF2NDTABLE
    INNER JOIN table2 ON table1.COLUMN1OFFIRSTTABLE = table2.COLUMN1OF2NDTABLE
    References :

Leave a Reply