Tyler Tyler - 18 days ago 7
MySQL Question

How do I update the value of a field in a row I just selected?

I used a

mysql_query
to select rows from my DB that contain part of a string and this works correctly as I am getting the correct rows echoed. I now want to take those rows where the
sku LIKE '%$string%'
and input the value
$string
into a different column in that row called
skugroup
. I have tried using insert and update and I just don't seem to be getting any results in the database. Here is how I am selecting the rows, which once again is working:

$string = 'BB-BB-2017-MAR';
$result = mysql_query("SELECT * FROM cruncher.sku WHERE sku LIKE '%$string%'");
if($result === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_array($result))
{
echo $row['sku'] . "</br>";
}

Answer

if you want to update value in another column, then you need to execute:

update cruncher.sku
set skugroup = sku
WHERE sku LIKE '%$string%'

if you want to create alias, then you need something like:

SELECT *, sku as skugroup FROM cruncher.sku WHERE sku LIKE '%$string%'