"""SQL FIRST() 离线实验:在内存 SQLite 中验证 ORDER BY ... ASC LIMIT 1 与 FIRST_VALUE() 窗口函数等效实现。"""
import json
import platform
import sqlite3
def run() -> dict:
conn = sqlite3.connect(":memory:")
conn.executescript(
"""
CREATE TABLE Websites (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
url TEXT NOT NULL,
alexa INTEGER NOT NULL,
country TEXT NOT NULL
);
INSERT INTO Websites (id, name, url, alexa, country) VALUES
(1, 'Google', 'https://www.google.cm/', 1, 'USA'),
(2, '淘宝', 'https://www.taobao.com/', 13, 'CN'),
(3, '蓝色天空', 'https://www.mbsky.com/', 4689, 'CN'),
(4, '微博', 'http://weibo.com/', 20, 'CN'),
(5, 'Facebook', 'https://www.facebook.com/', 3, 'USA'),
(6, '百度', 'https://www.baidu.com/', 4, 'CN'),
(7, 'stackoverflow', 'http://stackoverflow.com/', 0, 'IND');
"""
)
cases = []
# Case 1: LIMIT 1 & ORDER BY id ASC LIMIT 1
cur = conn.execute("SELECT name AS FirstSite FROM Websites ORDER BY id ASC LIMIT 1;")
alias = cur.description[0][0]
first_ordered = cur.fetchone()[0]
first_plain = conn.execute("SELECT name AS FirstSite FROM Websites LIMIT 1;").fetchone()[0]
exp1 = {
"alias": "FirstSite",
"ordered_first": "Google",
"plain_limit1_first": "Google",
}
act1 = {
"alias": alias,
"ordered_first": first_ordered,
"plain_limit1_first": first_plain,
}
cases.append(
{
"name": "select_first_site_limit_1",
"summary": "验证使用 ORDER BY id ASC LIMIT 1 与 LIMIT 1 取回 Websites 首条站点名 Google",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
}
)
# Case 2: Explicit ORDER BY determines what "first" means
by_id = conn.execute("SELECT name FROM Websites ORDER BY id ASC LIMIT 1;").fetchone()[0]
by_alexa = conn.execute("SELECT name FROM Websites ORDER BY alexa ASC LIMIT 1;").fetchone()[0]
by_name = conn.execute("SELECT name FROM Websites ORDER BY name ASC LIMIT 1;").fetchone()[0]
exp2 = {
"first_by_id": "Google",
"first_by_alexa": "stackoverflow",
"first_by_name": "Facebook",
}
act2 = {
"first_by_id": by_id,
"first_by_alexa": by_alexa,
"first_by_name": by_name,
}
cases.append(
{
"name": "order_by_affects_first_record",
"summary": "验证不同 ORDER BY 列(id / alexa / name)会改变“首条记录”结果,显式排序必不可少",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
}
)
# Case 3: Standard SQL window function FIRST_VALUE()
win_first = conn.execute(
"SELECT DISTINCT FIRST_VALUE(name) OVER (ORDER BY id ASC) FROM Websites;"
).fetchone()[0]
exp3 = {"first_value_window": "Google"}
act3 = {"first_value_window": win_first}
cases.append(
{
"name": "window_first_value_standard_sql",
"summary": "验证标准 SQL 窗口函数 FIRST_VALUE(name) OVER (ORDER BY id ASC) 返回 Google",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
}
)
conn.close()
return {
"version": f"CPython {platform.python_version()} / SQLite {sqlite3.sqlite_version}",
"cases": cases,
}
if __name__ == "__main__":
print(json.dumps(run(), ensure_ascii=False, indent=2))
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/sql-func-first-lab/explore-func-first.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-func-first.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联67章) · 含当前章节或实例 · mbskycom202612a1523.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1542.zip