ivanv Posted August 28, 2003 Share Posted August 28, 2003 I want to do an UPDATE on certain columns, WHERE a given column matches the result of a query, like this: UPDATE table SET column1 = value WHERE column2 = (SELECT columnx FROM table2 WHERE columny = value) I tried that but it doesn\'t work, so I\'m wondering if it\'s possible to do what I want, and how? Thanks in advance!! - Ivan V. Link to comment https://forums.phpfreaks.com/topic/952-is-this-possible-where-column-select/ Share on other sites More sharing options...
Barand Posted August 28, 2003 Share Posted August 28, 2003 UPDATE table SET column1 = value WHERE column2 = (SELECT columnx FROM table2 WHERE columny = value) I think subquery support add in MySQL v4.1 A work around could be [php:1:dcdad8e5a5]<?php $sql = \"SELECT columnx FROM table2 WHERE columny = value\"; $result = mysql_query($sql); while ($row = mysql_fetch_row($result)) { $array[] = $row[0]; } $vals = join(\',\' , $array); // if numeric - use next line if string val // $vals = \"\'\" . join ( \"\',\'\" , $array) . \"\'\" $sql2 = \"UPDATE table SET column1 = value WHERE column2 IN ($vals)\"; mysql_query $sql2); ?>[/php:1:dcdad8e5a5] hth Link to comment https://forums.phpfreaks.com/topic/952-is-this-possible-where-column-select/#findComment-3216 Share on other sites More sharing options...
ivanv Posted August 28, 2003 Author Share Posted August 28, 2003 Thanks for your answer... Although I was hoping I could do what I wanted without resorting to another language . - Ivan V. Link to comment https://forums.phpfreaks.com/topic/952-is-this-possible-where-column-select/#findComment-3219 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.