當前位置:編程學習大全網 - 網站源碼 - 如何在MySQL中查詢當前數據上壹條和下壹條的記錄

如何在MySQL中查詢當前數據上壹條和下壹條的記錄

您好,很高興為您解答。

方法壹:

查詢上壹條記錄的SQL語句(如果有其他的查詢條件記得加上other_conditions以免出現不必要的錯誤):

select?*?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];

查詢下壹條記錄的SQL語句(如果有其他的查詢條件記得加上other_conditions以免出現不必要的錯誤):

select?*?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];

方法二:

查詢上壹條記錄的SQL語句((如果有其他的查詢條件記得加上other_conditions以免出現不必要的錯誤))

select?*?from?table_a?where?id?=?(select?max(id)?from?table_a?where?id?<?{$id}?[and?other_conditions])?[and?other_conditions];

查詢下壹條記錄的SQL語句(如果有其他的查詢條件記得加上other_conditions以免出現不必要的錯誤):

select?*?from?table_a?where?id?=?(select?min(id)?from?table_a?where?id?>?{$id}?[and?other_conditions])?[and?other_conditions];

如若滿意,請點擊右側采納答案,如若還有問題,請點擊追問

希望我的回答對您有所幫助,望采納!

~?O(∩_∩)O~

  • 上一篇:ibatis解決了什麽問題
  • 下一篇:如何查看tomcat源碼中使用的是jdk的什麽版本
  • copyright 2024編程學習大全網