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

Réponse
 
LinkBack Outils de la discussion
Vieux 26/10/2007, 14h15   #1
Max
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Incrementally number result rows

Hello group,

How would I go about numbering rows from an SQL query?

For example, if I use the query:

SELECT myCol FROM myTable ORDER BY myOtherCol;

it returns:

+-------+
| row x |
| row y |
| row z |
+-------+

Now I would like to get:

SELECT [something], myCol FROM myTable ORDER BY myOtherCol;
+-----------+
| 1 | row x |
| 2 | row y |
| 3 | row z |
+-----------+

....or...

SELECT [something], myCol FROM myTable ORDER BY myOtherCol DESC;
+-----------+
| 1 | row z |
| 2 | row y |
| 3 | row x |
+-----------+



Thanks,
Max

  Réponse avec citation
Vieux 26/10/2007, 14h32   #2
Max
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Incrementally number result rows

On Oct 26, 9:15 am, Max <Maxime.Pla...@gmail.com> wrote:
> Hello group,
>
> How would I go about numbering rows from an SQL query?
>
> For example, if I use the query:
>
> SELECT myCol FROM myTable ORDER BY myOtherCol;
>
> it returns:
>
> +-------+
> | row x |
> | row y |
> | row z |
> +-------+
>
> Now I would like to get:
>
> SELECT [something], myCol FROM myTable ORDER BY myOtherCol;
> +-----------+
> | 1 | row x |
> | 2 | row y |
> | 3 | row z |
> +-----------+
>
> ...or...
>
> SELECT [something], myCol FROM myTable ORDER BY myOtherCol DESC;
> +-----------+
> | 1 | row z |
> | 2 | row y |
> | 3 | row x |
> +-----------+
>
> Thanks,
> Max


Answer to myself (I hope it can others)
taken from: http://forums.mysql.com/read.php?10,36490,36511

SET @row = 0;
SELECT @row := @row + 1 AS rowNumber, myCol FROM myTable ORDER BY
myOtherCol DESC;

  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 05h04.


É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,44008 seconds with 10 queries