sql - Postgres 9.2 find, compare and replace two values in column -
hi trying run query find 2 values in same column, compare them , set highest value both , bit confused.
i can not work out how prettily have gone ugly approach , still lost.
i have table data in bit this
fruitname fruitvalue apples 11 pears 10 oranges 2
i want able query table , values apples , pears. want replace value of apples or pears based on highest value
like this
fruitname fruitvalue apples 11 pears 11 oranges 2
the code below gets me highest value
select max (fruitvalue) fruit fruitname in ('apple','pear')
but can work out how turn result variable , assign fruitname.
for example
$variable = select max (fruitvalue) fruit fruitname in ('apple','pear') update fruittable set fruitvalue = '$variable' fruitname = 'apple'; update fruittable set fruitvalue = '$variable' fruitname = 'pears';
no need variable, can use select maximum value directly in update
statement.
update fruittable set fruitvalue = (select max (fruitvalue) fruit fruitname in ('apple','pear')); fruitname in ('apple', 'pear');
this works because sub-select returns 1 row.
Comments
Post a Comment