Jump to content

Is this possible? WHERE column = (SELECT ...)


ivanv

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.