PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > SELECT TOP * ORDER BY question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
SELECT TOP * ORDER BY question

Réponse
 
LinkBack Outils de la discussion
Vieux 31/12/2007, 18h50   #1
metaperl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut SELECT TOP * ORDER BY question

If I do

SELECT TOP 25 * FROM table ORDER BY id

does it select any 25 records and then order them

or does it SELECT all the records, order them by ID then return the
first 25?

I'm guessing the former (based on some comparative SELECTs) and think
I need to do this:

SELECT TOP 25
(SELECT * FROM table ORDER BY id)

to get what I want.
  Réponse avec citation
Vieux 31/12/2007, 19h17   #2
metaperl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SELECT TOP * ORDER BY question

On Dec 31, 1:50pm, metaperl <metap...@gmail.com> wrote:

>
> SELECT TOP 25
> (SELECT * FROM table ORDER BY id)


actually the above is invalid syntax. how can I get MS-SQL to give me
the 25 records which have the lowest id instead of 25 random records
ordered by id?
  Réponse avec citation
Vieux 31/12/2007, 19h20   #3
Gert-Jan Strik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SELECT TOP * ORDER BY question

metaperl,

Logically, this statement will select all rows from "table", sort them
on column id, select the first 25 of them, and return these rows.

If you would want the other behavior that you describe (which is
unlikely), you would have to write this:

SELECT *
FROM (
SELECT TOP 25 *
FROM table
ORDER BY NEWID()
) AS T
ORDER BY T.id

--
Gert-Jan


metaperl wrote:
>
> If I do
>
> SELECT TOP 25 * FROM table ORDER BY id
>
> does it select any 25 records and then order them
>
> or does it SELECT all the records, order them by ID then return the
> first 25?
>
> I'm guessing the former (based on some comparative SELECTs) and think
> I need to do this:
>
> SELECT TOP 25
> (SELECT * FROM table ORDER BY id)
>
> to get what I want.

  Réponse avec citation
Vieux 31/12/2007, 19h42   #4
David Portas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: SELECT TOP * ORDER BY question

"metaperl" <metaperl@gmail.com> wrote in message
news:bd9b28eb-22ad-4146-93e3-4dc8fdf4907c@w47g2000hsa.googlegroups.com...
> If I do
>
> SELECT TOP 25 * FROM table ORDER BY id
>
> does it select any 25 records and then order them
>
> or does it SELECT all the records, order them by ID then return the
> first 25?
>
> I'm guessing the former (based on some comparative SELECTs) and think
> I need to do this:
>
> SELECT TOP 25
> (SELECT * FROM table ORDER BY id)
>
> to get what I want.



Books Online is your friend:

"If the query includes an ORDER BY clause, the first expression rows, or
expression percent of rows, ordered by the ORDER BY clause are returned. If
the query has no ORDER BY clause, the order of the rows is arbitrary."

http://msdn2.microsoft.com/en-us/library/ms189463.aspx

This is a very silly syntax (invented by Microsoft and not part of standard
SQL) because it means the ORDER BY serves a double purpose, which leads to
confusion all round.

--
David Portas


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


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