Jump to content

Update MYSQL table based on value in other table


billinboston

Recommended Posts

I have been trying to update a table based on whether the values in a column equal a value in separate table within the database. 

 

All I have been able to do is the basic set value based on value inputed into the php code.  If someone knows how to query a value from a separate table and then use it to compare to the first table that would be greatly appreciated.  Thanks guys.

 

 

<?php

$con = mysql_connect("server","database","%*&^(*&(*&");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("database", $con);

 

 

mysql_query("UPDATE data SET Column_name = 'Value'

WHERE column_name = **value in another table** ");

 

 

mysql_close($con);

?>

 

 

Link to comment
Share on other sites

You can run two query strings or use a nested query.  The nested query is below, then the two query strings below that.

 

Nested:

<?
// Your connection string
mysql_select_db("DATABASE", $con);

mysql_query("UPDATE data SET Column_name = 'Value' 
WHERE column_name = (SELECT some_column FROM some_table LIMIT 1)");

mysql_close($con);
?>

2 queries:

<?
// Your connection string
mysql_select_db("DATABASE", $con);

$row=mysql_fetch_array(mysql_query("SELECT some_column FROM some_table WHERE some_condition LIMIT 1"));
$query=mysql_query("UPDATE data SET Column_name = 'Value' 
WHERE column_name = ' ".$row['some_column']." ' ");

mysql_close($con);
?>

 

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.