Web Hosting Talk







View Full Version : PHP/SQL Embedded Query


Goldfiles
02-08-2008, 07:52 PM
I need to do a double query, where one is embedded in the other. I am not sure how to format this code near the bottom.

$sql = mysql_query("UPDATE table SET valueA = 'hello'
WHERE ValueB = '2'
AND ValueC = '3'
AND ValueD IN (SELECT ValueD FROM NewTable)");

I don't believe an UPDATE can use a JOIN....may be wrong on that. So, I am trying to use an embedded query like I have above with the "IN" keyword. How do I format that exactly? Thanks.

Codelphious
02-09-2008, 12:50 AM
From the MySQL manual:



Single-table syntax:

UPDATE [LOW_PRIORITY] [I]tbl_name
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

Multiple-table syntax:

UPDATE [LOW_PRIORITY] [I]table_references
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_condition]

If you're trying to update multiple tables use the multiple-table syntax. You can use any join statement campatible with SELECT inside a multiple-table UPDATE. Here's an example:


UPDATE table1 INNER JOIN table2 ON table1.id=table2.id SET table1.value='value1', table2.value='value2' WHERE table1.value2>'value3'
I hope that helps.

Goldfiles
02-09-2008, 01:02 AM
it sure does. Thank you!