sql server按某欄位提取前幾行

時間 2022-04-25 12:02:15

1樓:匿名使用者

sqlserver提取前幾行可用top來實現。

如sale表中有如下資料:

要按money從低到高提取前三行,可用如下語句:

select top 3 * from sale order by money

查詢結果:

反之,如果按從money從高到低,可用如下語句:

select top 3 * from sale order by money desc

查詢結果:

2樓:匿名使用者

begin tran

set identity_insert c在/ /片語是使自動遞增主鍵的表,可以插入到表中的原始值,關閉主鍵來增大等效的功能,並允許值的插入。

insert into c(列1,列2)

選擇a.column1為列1

b.column1為列2

從a.columnx = b.columny / /兩個在a join b條件 set identity_insert c關閉/ /插入功能將自動遞增的主鍵重啟提交

3樓:匿名使用者

這個得用分析函式。可以我不會sql server,不敢瞎說。

oracle的我可以幫你寫乙個,參考一下吧。

select *

from

(select zt,spdm,xssl,kcsl,rank() over(partition by zt order by lz) rk from cc)

where rk < 3

4樓:方司晨

select top 6

......

......

order by zt,lz

sql中如何在where字句裡擷取某個欄位的前幾位字元

5樓:匿名使用者

改成下面這樣就可以了

where left(p.end_time.substring,4)='2012'

6樓:匿名使用者

如果是sqlserver:

where left(p.end_time,4) = '2012'

如果是oracle:

where substr(p.end_time,0,4) = '2012'

7樓:匿名使用者

year(p.end_time) = '2012'

8樓:匿名使用者

我的oracle要改為這樣才能查到值:

where substr(p.end_time,1,4) = '2012'

sql如何提取按某欄位區分的唯一行資料

9樓:

select * from customer a inner join

(select customernum,max(customerid) customerid from customer group by customernum

) b on a.customerid = b.customerid這種表連線查詢,如果沒有錯的話,效率應該是最好的了。

10樓:赤芬

select * from customerwhere (customerid in

(select min(customerid)from customer

group by name , customernum , fundaccount , mobile ))

fundaccount這一列不提取就如下

group by name , customernum , mobile ))

11樓:匿名使用者

select *

from customer

where customerid=(select max(customerid)

from customer as a

where a.customernum=customer.customernum )

12樓:匿名使用者

select customerid , name,mobile ,distinct(customernum ) from customer

13樓:翩翩葉隨風

select customerid,name,customernum,fundaccount,mobile from customer group by customernum

14樓:民工在此

select * from customer t

where rowid in (select max(rowid) from customer d where t.customernum=d.customernum )

可以參照這個看看

15樓:匿名使用者

select a,max(b) from table where c=d group by a;

從**中找出a和b兩列的資料,若a有重複資料,則只顯示一筆,同時b顯示最大的那個值。

如果有多個列的值有重複,但只需要顯示一筆,可以多加幾個列名,比如:

select a,b,max(c) from table where d=e group by a,b;

針對重複的值只顯示一筆,還有一種sql語法:

select distinct a,b from table where c=d;

若a、b有重複,則只顯示一筆。

16樓:

真不是很清楚lz後面補充那段意思

sql 查詢的時候擷取欄位在逗號前的部分

17樓:

1、查詢資料庫表所有的字段,直接利用select語句。

2、查詢資料庫表部分字段,可以利用select 字段 命令,select 欄位名 from 資料庫表。

3、按照條件查詢,利用where後面加條件,select 欄位名 from 資料庫表 where 條件。

4、查詢資料庫字段記錄不重複的結果,利用distinct 命令。

5、查詢資料庫表資料前多少條,可以利用top命令,select top 數字 * from 資料庫表。

6、查詢資料庫表有時為了區分字段,需要給字段起個別名,可以利用as,select 欄位名 as 欄位名 from 資料庫表。

18樓:

可以使用substring_index(column, ',', 2)方法來實現。

具體sql語句為:

select id,name,substring_index(address, ',', 2) from people;

substring_index(address, ',', 2)的意思是擷取第二個 ',' 之前的所有字元。

擴充套件資料1、substring(name,3) 擷取name這個字段 從第三個字元開始,之後的所有個字元

select substring('1234567',3)

結果:34567

2、substring(name, -4) 擷取name這個欄位的第 4 個字元位置(倒數)開始取,直到結束

select substring('1234567',-4)

結果:4567

3、substring(name, -4,2) 擷取name這個欄位的第 4 個字元位置(倒數)開始取,只擷取之後的2個字元

select substring('1234567',-4,2)

結果:45

擷取第二個 '.' 之前的所有字元

19樓:匿名使用者

需要用charindex函式及substring函式配合使用。

1、建立測試表、插入資料:

create table test

(id varchar(100));

insert into test values ('23489324,sadjfl')

insert into test values ('sdfaadfs,543224')

insert into test values ('asdf,weere')

2、執行語句:

select substring(id,1,(charindex(',',id)-1)) from test;

3、執行結果(達到截圖逗號前的部分):

語句說明:

charindex(',',id)代表取id欄位中逗號所在的位置。

charindex(',',id)-1代表取id欄位逗號位置前一位的位置(即不含逗號)。

substring(id,1,(charindex(',',id)-1))代表擷取id欄位,從第一位開始擷取,取的長度為id欄位逗號位置前一位的位置。

20樓:匿名使用者

insert into newtable([id],[name],[address])

(select [id],[name],left(address,charindex(',',address)+charindex(',',right(address,len(address)-charindex(',',address)))-1) as [address] from people)

sql server 如何對乙個欄位的某幾行求和

21樓:標標課堂

sql server資料中運算子與表示式

22樓:折柳成萌

select sum(id4),count(*) from a_temp ;

可以一句sql就直接查詢得到列的和以及記錄數。

該sql中sum(id4)是列id4的總和,count(*)是得到的資料總行數。

23樓:匿名使用者

寫乙個儲存過程吧,來的快些。

mysql中怎麼設定某字段為當前時間

mysql中時間欄位datetime怎麼判斷為空一般為空都用null表示,所以一句sql語句就可以。1select from 表名where 日期字段 isnull 這裡要注意null的用法,不可以用 null這樣的形式表示。set starttime now 就可以了,為當前時間 mysql中為乙...

thinkphp 怎麼獲取某字段的乙個值

res student field id,name select 你要查詢出所有的id和name 那麼條件就不寫了,否則不能查詢出所有的。最好在後面加個limit限制下資料量,萬一是大型 海量資料,你的查詢工具就卡了,瀏覽器刷屏了!php thinkphp3。2怎麼獲取某資料庫欄位的內容?2 在中,...

sqlserver欄位區分大小寫嗎

在sql server中預設對大小寫是不敏感的,例如fname peter 和fname peter 結果是一樣的。但有時候使用者會要求區分大小寫,如驗證密碼等。這種情況下的處理辦法就是在字段後加上 collate chinese prc cs as ws如 select from hrs empl...

請教php關於撒mysql某錶某字段中的數值更改為

if row 7 1616 else if row 3 1818 else echo row 3 王五 以上語句改為以下語句試試 if row 7 1616 row 3 張三 else if row 7 1818 row 3 李四 else row 3 王五 另外 你的語句 if row 7 161...

sqlserver如何給表和字段新增備註並查詢以

表新增和修改備註 新增 exec sp addextendedproperty n ms description n 表備註內容 n schema n dbo n table n 表名 修改 exec sp updateextendedproperty n ms description n 表備註內...