"""MySQL WHERE 子句离线实验:实测 6 类比较运算符、13 类常用条件模式(含 AND/OR/LIKE/IN/NOT/BETWEEN/IS NULL)以及大小写敏感与不敏感比较行为。"""
import json
import platform
import sqlite3
def build_db() -> sqlite3.Connection:
conn = sqlite3.connect(":memory:")
conn.executescript(
"""
CREATE TABLE users (id INTEGER PRIMARY KEY, username TEXT NOT NULL);
INSERT INTO users VALUES (1, 'test'), (2, 'demo'), (3, 'admin');
CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, category TEXT, price REAL);
INSERT INTO products VALUES
(1, 'Phone', 'Electronics', 299.00),
(2, 'Cable', 'Electronics', 25.00),
(3, 'Jacket', 'Clothing', 80.00);
CREATE TABLE orders (id INTEGER PRIMARY KEY, order_date TEXT, total_amount REAL);
INSERT INTO orders VALUES
(1, '2022-11-05', 450.00),
(2, '2023-06-15', 1200.00),
(3, '2024-02-01', 300.00);
CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, salary INTEGER, department TEXT);
INSERT INTO employees VALUES
(1, 'Alice', 60000, 'Engineering'),
(2, 'Bob', 45000, NULL),
(3, 'Carol', 50000, 'Sales');
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);
INSERT INTO students VALUES (1, 'Lin', 20), (2, 'Chen', 21), (3, 'Zhao', 23);
CREATE TABLE customers (id INTEGER PRIMARY KEY, first_name TEXT, email TEXT);
INSERT INTO customers VALUES
(1, 'Jack', 'jack@example.com'),
(2, 'Julia', NULL),
(3, 'Mike', 'mike@example.com');
CREATE TABLE countries (code TEXT PRIMARY KEY);
INSERT INTO countries VALUES ('US'), ('CA'), ('MX'), ('CN');
CREATE TABLE articles_tbl (
article_id INTEGER PRIMARY KEY,
article_title TEXT NOT NULL,
article_author TEXT NOT NULL,
submission_date TEXT NOT NULL
);
INSERT INTO articles_tbl VALUES
(1, '学习 PHP', '蓝色天空', '2017-04-12'),
(2, '学习 MySQL', '蓝色天空', '2017-04-12'),
(3, 'JAVA 教程', 'MBSKY.COM', '2016-05-06'),
(4, '学习 Python', 'MBSKY.COM', '2016-03-06');
"""
)
return conn
def run() -> dict:
conn = build_db()
cases = []
# Case 1: Comparison operators table (=, <>, !=, >, <, >=, <=) with A=10, B=20
a, b = 10, 20
cmp_row = conn.execute(
"SELECT (? = ?), (? <> ?), (? != ?), (? > ?), (? < ?), (? >= ?), (? <= ?);",
(a, b, a, b, a, b, a, b, a, b, a, b, a, b),
).fetchone()
exp1 = {"eq": 0, "ne_angle": 1, "ne_bang": 1, "gt": 0, "lt": 1, "ge": 0, "le": 1}
act1 = dict(zip(["eq", "ne_angle", "ne_bang", "gt", "lt", "ge", "le"], cmp_row))
cases.append(
{
"name": "where_comparison_operators_a10_b20",
"summary": "以 A=10、B=20 实测 =、<>、!=、>、<、>=、<= 全部比较运算符布尔结果",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
}
)
# Case 2: 13 practical WHERE clause patterns
res_eq = [r[0] for r in conn.execute("SELECT username FROM users WHERE username = 'test';").fetchall()]
res_ne = [r[0] for r in conn.execute("SELECT username FROM users WHERE username != 'demo' ORDER BY id;").fetchall()]
res_gt = [r[0] for r in conn.execute("SELECT name FROM products WHERE price > 50.00 ORDER BY id;").fetchall()]
res_lt = [r[0] for r in conn.execute("SELECT id FROM orders WHERE order_date < '2023-01-01';").fetchall()]
res_ge = [r[0] for r in conn.execute("SELECT name FROM employees WHERE salary >= 50000 ORDER BY id;").fetchall()]
res_le = [r[0] for r in conn.execute("SELECT name FROM students WHERE age <= 21 ORDER BY id;").fetchall()]
res_and = [r[0] for r in conn.execute("SELECT name FROM products WHERE category = 'Electronics' AND price > 100.00;").fetchall()]
res_or = [r[0] for r in conn.execute("SELECT id FROM orders WHERE order_date >= '2023-01-01' OR total_amount > 1000.00 ORDER BY id;").fetchall()]
res_like = [r[0] for r in conn.execute("SELECT first_name FROM customers WHERE first_name LIKE 'J%' ORDER BY id;").fetchall()]
res_in = [r[0] for r in conn.execute("SELECT code FROM countries WHERE code IN ('US', 'CA', 'MX') ORDER BY code;").fetchall()]
res_not = [r[0] for r in conn.execute("SELECT name FROM products WHERE NOT category = 'Clothing' ORDER BY id;").fetchall()]
res_between = [r[0] for r in conn.execute("SELECT id FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';").fetchall()]
res_null = [r[0] for r in conn.execute("SELECT name FROM employees WHERE department IS NULL;").fetchall()]
res_not_null = [r[0] for r in conn.execute("SELECT first_name FROM customers WHERE email IS NOT NULL ORDER BY id;").fetchall()]
exp2 = {
"eq": ["test"],
"ne": ["test", "admin"],
"gt": ["Phone", "Jacket"],
"lt": [1],
"ge": ["Alice", "Carol"],
"le": ["Lin", "Chen"],
"and": ["Phone"],
"or": [2, 3],
"like": ["Jack", "Julia"],
"in": ["CA", "MX", "US"],
"not": ["Phone", "Cable"],
"between": [2],
"is_null": ["Bob"],
"is_not_null": ["Jack", "Mike"],
}
act2 = {
"eq": res_eq,
"ne": res_ne,
"gt": res_gt,
"lt": res_lt,
"ge": res_ge,
"le": res_le,
"and": res_and,
"or": res_or,
"like": res_like,
"in": res_in,
"not": res_not,
"between": res_between,
"is_null": res_null,
"is_not_null": res_not_null,
}
cases.append(
{
"name": "where_thirteen_condition_patterns",
"summary": "逐一实测等于、不等、大小、AND/OR、LIKE、IN、NOT、BETWEEN、IS NULL 与 IS NOT NULL 条件",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
}
)
# Case 3: Case-insensitive (NOCASE ~ MySQL default ci collation) vs case-sensitive (BINARY ~ MySQL COLLATE _bin / BINARY)
ci_lower = [
r[0]
for r in conn.execute(
"SELECT article_id FROM articles_tbl WHERE article_author = 'mbsky.com' COLLATE NOCASE ORDER BY article_id;"
).fetchall()
]
cs_lower = [
r[0]
for r in conn.execute(
"SELECT article_id FROM articles_tbl WHERE article_author = 'mbsky.com' COLLATE BINARY ORDER BY article_id;"
).fetchall()
]
cs_upper = [
r[0]
for r in conn.execute(
"SELECT article_id FROM articles_tbl WHERE article_author = 'MBSKY.COM' COLLATE BINARY ORDER BY article_id;"
).fetchall()
]
zh_match = [
r[0]
for r in conn.execute(
"SELECT article_id FROM articles_tbl WHERE article_author = '蓝色天空' ORDER BY article_id;"
).fetchall()
]
exp3 = {
"ci_lowercase_hits": [3, 4],
"cs_lowercase_hits": [],
"cs_uppercase_hits": [3, 4],
"zh_author_hits": [1, 2],
}
act3 = {
"ci_lowercase_hits": ci_lower,
"cs_lowercase_hits": cs_lower,
"cs_uppercase_hits": cs_upper,
"zh_author_hits": zh_match,
}
cases.append(
{
"name": "where_case_sensitivity_and_collation",
"summary": "对比不区分大小写(NOCASE/ci)与严格区分大小写(BINARY/cs)在字符串等值过滤中的差异",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
}
)
conn.close()
return {
"version": f"CPython {platform.python_version()} / SQLite {sqlite3.sqlite_version} (offline MySQL verifier)",
"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/mysql-where-clause-lab/explore-where-clause.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-where-clause.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联41章) · 含当前章节或实例 · mbskycom202612a1590.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1629.zip