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 > consecutive number question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
consecutive number question

Réponse
 
LinkBack Outils de la discussion
Vieux 06/02/2008, 22h09   #1
Jeff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut consecutive number question


I'm new to sql and mysql, so forgive me if I'm not asking this in the best
way.


I have a table with a column that should contain a list of integers from 1
to the number of rows that identify the order of the rows as they should
appear elsewhere in an application. I need to do some error checking to see
if all numbers are present or if there are any missing or any duplicates.
That's easy enough through the application, but don't know how to write the
sql to correct the problem if it is found. Could someone tell me how to
change the original column below into the desired one?

Thanks

Jeff

original
1
2
3
3
5
8


desired
1
2
3
4
5
6



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 06/02/2008, 22h28   #2
strawberry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: consecutive number question

On Feb 6, 10:09 pm, "Jeff" <no_...@george.com> wrote:
> I'm new to sql and mysql, so forgive me if I'm not asking this in the best
> way.
>
> I have a table with a column that should contain a list of integers from 1
> to the number of rows that identify the order of the rows as they should
> appear elsewhere in an application. I need to do some error checking to see
> if all numbers are present or if there are any missing or any duplicates.
> That's easy enough through the application, but don't know how to write the
> sql to correct the problem if it is found. Could someone tell me how to
> change the original column below into the desired one?
>
> Thanks
>
> Jeff
>
> original
> 1
> 2
> 3
> 3
> 5
> 8
>
> desired
> 1
> 2
> 3
> 4
> 5
> 6
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com


Assuming you have a table called orders(id*,orderdate,customer)

CREATE TABLE temp AS
SELECT COUNT(t2.id),t1.orderdate,t1.customer
FROM orders AS t1
LEFT JOIN orders AS t2
ON t2.id <= t1.id
GROUP BY t1.id;
  Réponse avec citation
Vieux 07/02/2008, 01h20   #3
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: consecutive number question

On Wed, 06 Feb 2008 23:09:08 +0100, Jeff <no_one@george.com> wrote:

>
> I'm new to sql and mysql, so forgive me if I'm not asking this in the
> best
> way.
>
>
> I have a table with a column that should contain a list of integers from
> 1
> to the number of rows that identify the order of the rows as they should
> appear elsewhere in an application. I need to do some error checking to
> see
> if all numbers are present or if there are any missing or any duplicates.
> That's easy enough through the application, but don't know how to write
> the
> sql to correct the problem if it is found. Could someone tell me how to
> change the original column below into the desired one?
>
> Thanks
>
> Jeff
>
> original
> 1
> 2
> 3
> 3
> 5
> 8
>
>
> desired
> 1
> 2
> 3
> 4
> 5
> 6


SET @myvar := 0;
UPDATE tablname SET fieldname = @myvar:= @myvar + 1 ORDER BY fieldname;
--
Rik Wasmus
  Réponse avec citation
Vieux 07/02/2008, 03h00   #4
Jeff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: consecutive number question


"Rik Wasmus" <luiheidsgoeroe@hotmail.com> wrote in message
newsp.t54rcgks5bnjuv@metallium.lan...
On Wed, 06 Feb 2008 23:09:08 +0100, Jeff <no_one@george.com> wrote:

>SET @myvar := 0;

UPDATE tablname SET fieldname = @myvar:= @myvar + 1 ORDER BY fieldname;
--
Rik Wasmus


Yup. It worked. So simple.

Thanks

Jeff



--
Posted via a free Usenet account from http://www.teranews.com

  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 22h13.


É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,10546 seconds with 12 queries