PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > update field with increment values
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
update field with increment values

Réponse
 
LinkBack Outils de la discussion
Vieux 01/01/2008, 13h07   #1
php_mysql_beginer911
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut update field with increment values

Hi everyone ..

i am wondering if it is possible to update a field where filed values
are null

with increment values

i can do same in php by using a loop and updating a field

for($i=1;$i<=mysql_num_rows($result);$i++ )
{
$sql="UPDATE table SET col=$i ORDER BY col_date DESC";
}

is it possible wihout using any php loop

i use when fields have values

update table set col=col+1 where .....

but couldn't find anywhere how to do when values are 0 or null with
only mysql query

hope somebody will reply
thanks
  Réponse avec citation
Vieux 01/01/2008, 14h18   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: update field with increment values

On Tue, 01 Jan 2008 14:07:54 +0100, php_mysql_beginer911
<deepakgc@gmail.com> wrote:

> Hi everyone ..
>
> i am wondering if it is possible to update a field where filed values
> are null
>
> with increment values
>
> i can do same in php by using a loop and updating a field
>
> for($i=1;$i<=mysql_num_rows($result);$i++ )
> {
> $sql="UPDATE table SET col=$i ORDER BY col_date DESC";


I don't think that's what you want...

> }
>
> is it possible wihout using any php loop
>
> i use when fields have values
>
> update table set col=col+1 where .....
>
> but couldn't find anywhere how to do when values are 0 or null with
> only mysql query


I'm going to guess this is what you want:

SET @myVar := 0;
UPDATE table SET col = @myVar := @myVar + 1 ORDER BY col_date DESC;

So, just 2 'queries' to call in MySQL.
--
Rik Wasmus
  Réponse avec citation
Vieux 02/01/2008, 16h21   #3
Norman Peelman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: update field with increment values

Rik Wasmus wrote:
> On Tue, 01 Jan 2008 14:07:54 +0100, php_mysql_beginer911
> <deepakgc@gmail.com> wrote:
>
>> Hi everyone ..
>>
>> i am wondering if it is possible to update a field where filed values
>> are null
>>
>> with increment values
>>
>> i can do same in php by using a loop and updating a field
>>
>> for($i=1;$i<=mysql_num_rows($result);$i++ )
>> {
>> $sql="UPDATE table SET col=$i ORDER BY col_date DESC";

>
> I don't think that's what you want...
>
>> }
>>
>> is it possible wihout using any php loop
>>
>> i use when fields have values
>>
>> update table set col=col+1 where .....
>>
>> but couldn't find anywhere how to do when values are 0 or null with
>> only mysql query

>
> I'm going to guess this is what you want:
>
> SET @myVar := 0;
> UPDATE table SET col = @myVar := @myVar + 1 ORDER BY col_date DESC;
>
> So, just 2 'queries' to call in MySQL.
> --Rik Wasmus



It should work just fine if the column value is 0, NULL is the issue.
Since NULL is not a value there is nothing to add 1 to. I would have to
say this is the expected behavior. But, I suppose it would be ok if it
worked that way too.



--
Norman
Registered Linux user #461062
  Réponse avec citation
Vieux 02/01/2008, 16h28   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: update field with increment values

On Wed, 02 Jan 2008 17:21:26 +0100, Norman Peelman <npeelman@cfl.rr.com>
wrote:

> Rik Wasmus wrote:
>> On Tue, 01 Jan 2008 14:07:54 +0100, php_mysql_beginer911
>> <deepakgc@gmail.com> wrote:
>>
>>> Hi everyone ..
>>>
>>> i am wondering if it is possible to update a field where filed values
>>> are null
>>>
>>> with increment values
>>>
>>> i can do same in php by using a loop and updating a field
>>>
>>> for($i=1;$i<=mysql_num_rows($result);$i++ )
>>> {
>>> $sql="UPDATE table SET col=$i ORDER BY col_date DESC";

>> I don't think that's what you want...
>>
>>> }
>>>
>>> is it possible wihout using any php loop
>>>
>>> i use when fields have values
>>>
>>> update table set col=col+1 where .....
>>>
>>> but couldn't find anywhere how to do when values are 0 or null with
>>> only mysql query

>> I'm going to guess this is what you want:
>> SET @myVar := 0;
>> UPDATE table SET col = @myVar := @myVar + 1 ORDER BY col_date DESC;
>> So, just 2 'queries' to call in MySQL.

>
> It should work just fine if the column value is 0, NULL is the issue.
> Since NULL is not a value there is nothing to add 1 to. I would have to
> say this is the expected behavior. But, I suppose it would be ok if it
> worked that way too.


You're not working with the previous column value, so wether 'col' holds
NULL,0, or 'thisisjustastring', it will just be overwritten with the
integer from @myVar. You're effectively making an entire new 'order' with
this. If previous values have to be taken into account I have
misunderstood the question.
--
Rik Wasmus
  Réponse avec citation
Vieux 04/01/2008, 20h13   #5
Norman Peelman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: update field with increment values

Rik Wasmus wrote:
> On Wed, 02 Jan 2008 17:21:26 +0100, Norman Peelman <npeelman@cfl.rr.com>
> wrote:
>
>> Rik Wasmus wrote:
>>> On Tue, 01 Jan 2008 14:07:54 +0100, php_mysql_beginer911
>>> <deepakgc@gmail.com> wrote:
>>>
>>>> Hi everyone ..
>>>>
>>>> i am wondering if it is possible to update a field where filed values
>>>> are null
>>>>
>>>> with increment values
>>>>
>>>> i can do same in php by using a loop and updating a field
>>>>
>>>> for($i=1;$i<=mysql_num_rows($result);$i++ )
>>>> {
>>>> $sql="UPDATE table SET col=$i ORDER BY col_date DESC";
>>> I don't think that's what you want...
>>>
>>>> }
>>>>
>>>> is it possible wihout using any php loop
>>>>
>>>> i use when fields have values
>>>>
>>>> update table set col=col+1 where .....
>>>>
>>>> but couldn't find anywhere how to do when values are 0 or null with
>>>> only mysql query
>>> I'm going to guess this is what you want:
>>> SET @myVar := 0;
>>> UPDATE table SET col = @myVar := @myVar + 1 ORDER BY col_date DESC;
>>> So, just 2 'queries' to call in MySQL.

>>
>> It should work just fine if the column value is 0, NULL is the
>> issue. Since NULL is not a value there is nothing to add 1 to. I would
>> have to say this is the expected behavior. But, I suppose it would be
>> ok if it worked that way too.

>
> You're not working with the previous column value, so wether 'col' holds
> NULL,0, or 'thisisjustastring', it will just be overwritten with the
> integer from @myVar. You're effectively making an entire new 'order'
> with this. If previous values have to be taken into account I have
> misunderstood the question.
> --Rik Wasmus


I'm sorry, I meant that as a response to the OP...

--
Norman
Registered Linux user #461062
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h20.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,29833 seconds with 13 queries