當前位置:首頁 » 編程語言 » sql上下記錄
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql上下記錄

發布時間: 2023-03-16 13:25:24

㈠ 請教一段能同時查出上一條記錄及下一條記錄的sql

如果有主鍵自增或者按時間排序的資料庫記錄的話
直接根據當前的記錄的主鍵或時間,大概可以這樣寫
(select * from table where id > 當前id order by id asc limit 1 ) union
(select * from table where id < 當前id order by id desc limit 1)
這樣差不多能滿足

㈡ 關於查詢sql中數據上一條記錄和下一條記錄的sql語句......

sql中是設置的自增列啊;不需輸入當前的id,這里默認的是1;要是是1的話,就只能查到第2個,但3個就不曉得了回答: 當你查到第2個的時候,就獲取第二條的ID,點下一條的時候,就用第二條的id去查,後邊的類推~追問: 主要就是現在的id的獲取問題回答: 第一條就以0開始就行,後邊就獲取查出來的數據的id就行了, 你數據都查出來了,還獲取不到id?追問: id = Request.QueryString["id"]; 我是用這個獲取的;不知對否回答: 你能獲取到值就可以了,調試一下就行了~追問: 關鍵就是無法獲取到值..問題就出現在這里了,卡起了回答:

㈢ [sql]上下記錄間相同的數,如何統計!求助高手!~

這蘆悔么有難度的題一分都沒有哦
表裡面一定要加個排序列!不然消嘩碼沒有規則
/*
-----建表
create table aa(
id int not null identity(1,1) primary key,
n1 int not null,
n2 int not null,
n3 int not null,
n4 int not null
)
---數據
insert into aa(n1,n2,n3,n4) values(5,17,20,25)
insert into aa(n1,n2,n3,n4) values(1,15,25,30)
insert into aa(n1,n2,n3,n4) values(2,3,11,26)
insert into aa(n1,n2,n3,n4) values(3,11,26,50)
insert into aa(n1,n2,n3,n4) values(10,12,26,29)
*/
---查拿哪詢
select a.* ,
isnull((
select convert(varchar(16),disb.n)+',' from
(select n1 as n from aa where id=a.id or id=a.id-1 union all
select n2 as n from aa where id=a.id or id=a.id-1 union all
select n3 as n from aa where id=a.id or id=a.id-1 union all
select n4 as n from aa where id=a.id or id=a.id-1 )disb group by n having count(*)>1 order by n for xml path('')
),'')n,
(select count(n) from (
select n from
(select n1 as n from aa where id=a.id or id=a.id-1 union all
select n2 as n from aa where id=a.id or id=a.id-1 union all
select n3 as n from aa where id=a.id or id=a.id-1 union all
select n4 as n from aa where id=a.id or id=a.id-1 )disb group by n having count(*)>1
)n)gs
from aa a

--結果
id n1 n2 n3 n4 n gs
1 5 17 20 25 0
2 1 15 25 30 25, 1
3 2 3 11 26 0
4 3 11 26 50 3,11,26, 3
5 10 12 26 29 26, 1

(5 行受影響)

網路lj 結果對不齊

㈣ 關於查詢sql中數據上一條記錄和下一條記錄的sql語句......

可用row_number來解決。

如student表

id name create_date

1 張三 2015-07-01

2 李四 2015-06-01

3 王五 2015-08-01

4 趙六 2015-04-01


如,要查找張三的create_date前和後各一條數據。

withtas
(selectstudent.*,row_number()over(orderbycreate_date)rnfromstudent)
select*fromstudentwherern=(selectt.rn+1fromtwheret.name='張三')
unionall
select*fromstudentwherern=(selectt.rn-1fromtwheret.name='張三')

結果應是:

id name create_date

2 李四 2015-06-01

3 王五 2015-08-01

㈤ SQL如何獲取上一條..下一條..首尾記錄...

獲得上一條的id :select max(id)as id from [表] where id<"[你的要查的id]" order by [.....]

獲得下一條的id :select min(id)as id from [表] where id>"[你的要查的id]" order by [.....]

很笨的辦法但是很直觀·
不知道你是什麼資料庫··根據不同的資料庫有很多不同的寫法··
比如 mysql 中的 limit 或者 mssql 中的 top
寫法多了去啦··呵呵··上面舉個例子罷了··希望對你有幫助

㈥ Access資料庫的上下條記錄能相互計算嗎比如用SQL查詢:當前記錄與下一條記錄的比值=已知值。請幫助解答

可以的,放在where條件中就可以了!緩殲孝

select * from tablename where tablename.col1/tablename2 = value
如果類型不同需要進行一次轉換的!

如col1是int,而col2也是int,你要的值是一個其他類型時,就用:

select* from tablename where convert(類型,col1/col2[,長度]) = value

也就是說還要會用convert進行類型之間的強制轉換!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
看錯了,你說的上下條記錄是吧?

答案是沒有任何辦法的。因為在關系型資料庫中是不存在下下條關系的,每條記錄都相對一個實體,而記錄與記錄之間沒有任何關系的。只是習慣上我們可以對其進行排序,但無法定義進具體的上下條的,(存儲順序還是某一條件的排序?)關系資料庫在設計時就不考慮某相互之間的關系,也就沒有所謂的上下條的,你可以看到換一種排序方法,上下的條的關系就改變了,擾稿或者說不同的順序表示的資料庫是一樣的。所以沒有具體實體間聯系。

不過可以程改源序進行實現這種不符合資料庫數據的辦法的!

㈦ 誰給我寫個sql語句:關於上一條記錄和下一條記錄的對比

select b.*,
(case when (b.s_time-a.s_time)*1440>2 then 2
else 0 end ) --下一條記錄與上一條記錄的時間差--分鍾
from
(select a.*,
row_number() over(partition by servier_id order by service_id,s_time desc) roworder
from t1
) a,
(select a.*,
row_number() over(partition by servier_id order by service_id,s_time desc) roworder
from t1
) b
where a.servicer_id=b.service_id and b.roworder=a.roworder+1;

其中
t1表名
servier_id 是一個關鍵,比如一個員工的ID
s_time 記錄時間
做兩個子查詢,對每一個員工的記錄都按時間倒序排列
where條件把同一員工的前後兩條記錄關聯起來。

㈧ sql語句查詢,某一記錄上下相鄰的兩條記錄。怎麼寫

select * from news a
where news_id > 12345 and not exists(select 1 from news where news_id > 12345 and
news_id < a.id )
or
news_id < 12345 and not exists(select 1 from news where news_id < 12345 and
news_id > a.id )

如果不需要用一條語句寫出來,那分開寫的話,效率會好一些,這樣寫必須用一個or關鍵字
分開寫:
select max(news_id) from news where news_id < 12345
select min(news_id) from news where news_id > 12345

㈨ SQL 求一列中上下記錄差的絕對值

select a.id,a.n, ( select b.n-a.n from aa b where b.id=a.id+1) cha
from aa a
where ...

差不多這樣了,我沒有測試。 提醒到晌如這樣,應該你可以debug出來了。
對了,要絕對值的話亮滑,找宴鍵啟abs用下一下即可。

㈩ 如何獲取SQL查詢當前數據上一條和下一條的記錄

方法一:x0dx0a查詢上一條記錄的SQL語句(如果有其他的查詢條件記得加上other_conditions以免出現不必要的錯誤):x0dx0a1x0dx0aselect * from table_a where id = (select id from table_a where id < {$id} [and other_conditions] order by id desc limit 1) [and other_conditions];x0dx0a查詢下一條記錄的SQL語句(如果有其他的查詢條件記得加上other_conditions以免出現不必要的錯誤):x0dx0a1x0dx0aselect * from table_a where id = (select id from table_a where id > {$id} [and other_conditions] order by id asc limit 1) [and other_conditions];