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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.