對查詢結果隨機排序

2021-06-19 21:05:19 字數 3441 閱讀 1078

為了對行進行隨機排序,或者返回隨機選擇的x行資料,你可以在select語句中使用rand函式。但是rand函式在整個查詢中只被計算一次,所以所有的行都具有相同的值。你可以使用order by子句根據從newid函式返回的結果來排序行,如以下**所示:

select *

from northwind..orders

order bynewid()

select top 10 *

from northwind..orders

order bynewid()

在這裡,最關鍵的是newid()函式

建立uniqueidentifier型別的唯一值。

語法newid( )

返回型別

uniqueidentifier

示例a.對變數使用 newid 函式

下面的示例使用 newid 對宣告為uniqueidentifier資料型別的變數賦值。在測試該值前,將先列印uniqueidentifier資料型別變數的值。

-- creating a local variable with declare/set syntax.

declare @myid uniqueidentifier

set @myid = newid()

print 'value of @myid is: '+ convert(varchar(255), @myid)

下面是結果集:

value of @myid is: 6f9619ff-8b86-d011-b42d-00c04fc964ff

說明對於每台計算機,由 newid 返回的值不同。所顯示的數字僅起解釋說明的作用。

b.在 create table 語句中使用 newid

下面的示例建立具有uniqueidentifier資料型別的cust表,並使用 newid 將預設值填充到表中。為 newid() 賦預設值時,每個新行和現有行均具有cust_id列的唯一值。

-- creating a table using newid for uniqueidentifier data type. 

create table cust

( cust_id uniqueidentifier not null

default newid(),

company varchar(30) not null,

contact_name varchar(60) not null,

address varchar(30) not null,

city varchar(30) not null,

state_province varchar(10) null,

postal_code varchar(10) not null,

country varchar(20) not null,

telephone varchar(15) not null,

fax varchar(15) null)go

-- inserting data into cust table.

insert cust

(cust_id, company, contact_name, address, city, state_province,

postal_code, country, telephone, fax)

values

(newid(), 'wartian herkku', 'pirkko koskitalo', 'torikatu 38', 'oulu', null,

'90110', 'finland', '981-443655', '981-443655')

insert cust

(cust_id, company, contact_name, address, city, state_province,

postal_code, country, telephone, fax)

values

(newid(), 'wellington importadora', 'paula parente', 'rua do mercado, 12', 'resende', 'sp',

'08737-363', 'brazil', '(14) 555-8122', '')

insert cust

(cust_id, company, contact_name, address, city, state_province,

postal_code, country, telephone, fax)

values

(newid(), 'cactus comidas para ilevar', 'patricio simpson', 'cerrito 333', 'buenos aires', null,

'1010', 'argentina', '(1) 135-5555', '(1) 135-4892')

insert cust

(cust_id, company, contact_name, address, city, state_province,

postal_code, country, telephone, fax)

values

(newid(), 'ernst handel', 'roland mendel', 'kirchgasse 6', 'graz', null,

'8010', 'austria', '7675-3425', '7675-3426')

insert cust

(cust_id, company, contact_name, address, city, state_province,

postal_code, country, telephone, fax)

values

(newid(), 'maison dewey', 'catherine dewey', 'rue joseph-bens 532', 'bruxelles', null,

'b-1180', 'belgium', '(02) 201 24 67', '(02) 201 24 68')

go

c. 使用 uniqueidentifier 和變數賦值

下面的示例宣告區域性變數@myiduniqueidentifier資料型別。然後使用 set 語句為該變數賦值。

declare @myid uniqueidentifier 

set @myid = 'a972c577-dfb0-064e-1189-0154c99310daac12'

go

MySQL對查詢結果排序

從表中查詢出來的資料,可能是無序的,或者其排列順序表示使用者期望的 使用order by對查詢結果進行排序 select 欄位名1,欄位名2,from 表名 order by 欄位名1 asc desc 欄位名2 asc desc 指定的欄位名1 欄位名2,是對查詢結果排序的依據 引數asc,表示按...

對查詢結果進行排序

order by 列名 asc desc 公升序或降序排序單列排序 多列排序 指定排序方向 一 單列排序 select from bookinfo order by price desc 注意 預設為公升序排序 二 多列排序 按照多個列進行排序,預設公升序,如 相同 時,按庫存排序 select f...

MySQL對查詢結果排序

mysql對查詢結果排序,從表中查詢出來的資料,可能是無序的,或者其排列順序表示使用者期望的。使用order by對查詢結果進行排序 select 欄位名1,欄位名2,from 表名 order by 欄位名1 asc desc 欄位名2 asc desc 指定的欄位名1 欄位名2,是對查詢結果排序...