SQL語句查詢問題,高手請現身,一個SQL語句查詢問題,高手請現身

2022-04-07 19:40:01 字數 1570 閱讀 8102

1樓:

因sql表沒有記錄號屬性,所以所謂第一行到第n行,在沒有給出排序條件的情況下,是不能確定的記錄集.

所以,您需要給出排序條件或對錶按一定的欄位進行了索引,才能確定哪一條記錄是第幾行.

比如,您是要按a排序,那不就是全部記錄嗎?就不需要查詢條件了.

如果是按b排序或索引,即麼可以這樣求您的記錄集

select * from t as g where b<=

(select top 1 b from t where a=(select max(a) from t) )

order by b

就是先求出a的最大值對應的b值,

再從按b排序記錄集中去掉取上面求出的b值大的(排在後面的)記錄.

是這意思嗎?

***********************************==

但是您沒有認真看答案,sql server 表不象vfp表有固定順序,所以沒有排序規則無法確定是某一行是第幾行,必須要給出排序欄位才行.

如果排序欄位是b,則這樣可以生成新表:

select * into g from t where b<=

(select top 1 b from t where a=(select max(a) from t) )

如果是vfp,

則這樣select * into g from t where recno()<=

(select recno() from t where a=(select max(a) from t) )

2樓:獨駕舟千里去

insert into g

select a,b from t

order by a;

select * from g

order by a

你這個需求除了寫函式以外絕大多數資料庫不能滿足,因為絕大多數資料庫沒有rowid這個屬性

3樓:匿名使用者

輸出 :

select a, b from g

union all

select a, b from t

order by a asc

輸入:insert into g

select distinct a, b from torder by a asc解決

4樓:匿名使用者

insert into g (select a,b from (select a,b from t order by a asc))

5樓:佳樂比海

如果t表只有兩個欄位(a,b),那麼查出來a的最大值便是t表的最後一行(可能插入時並不是排好順序的)。

那麼,按照題意,g表中的的資料應該和t表一致。

6樓:匿名使用者

建立g表,結構和資料**於t表。

create table g as select top n from t;

7樓:

select top n a,b into g from t order by a

求高手編寫SQL查詢語句急

個人認為樓上的回答有2個問題 第7小題,如果高學分課程這張表存在,那sql語句會出錯,應該先要用if not exist語句判斷這張表是否存在,如果存在,則用drop table語句刪除這張表才執行建立表的語句。第8小題應為select from 學生表 where year 出生日期 1993 o...

跪求sql語句高手查詢整個資料庫中的特定字元

declare str nvarchar 10 declare tablename varchar 50 declare colname varchar 50 declare counts int declare sql nvarchar 2000 以上定義變數 declare cur1 curso...

求一條sql多條件查詢語句

假設子女孩次為1或2的話 select 姓名,max case 子女孩次 when 1 then 子女姓名 else null end 一孩姓名,max case 子女孩次 when 1 then 子女性別 else null end 一孩性別,max case 子女孩次 when 2 then 子...