2.Start with

Start with...Connect By子句递归查询一般用于一个表维护树形结构的应用。
创建示例表:
CREATE TABLE TBL_TEST
(
  ID    NUMBER,
  NAME  VARCHAR2(100 BYTE),
  PID   NUMBER                                  DEFAULT 0
);
 
插入测试数据:
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('1','10','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('2','11','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('3','20','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('4','12','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('5','121','2');
 
从Root往树末梢递归
select * from TBL_TEST
 start with id=1
 connect by prior id = pid
 
从末梢往树ROOT递归
select * from TBL_TEST
 start with id=5
 connect by prior pid = id
资料引用:http://www.knowsky.com/390130.html

相关函数:SYS_CONNECT_BY_PATH(a,',')(用逗号连接父节点到当前行)
select SYS_CONNECT_BY_PATH(id,',') from TBL_TEST
 start with id=5
 connect by prior pid = id

你提到的 `字符串.startwith(、)` 看起来是想使用 Python 中判断字符串是否以某个前缀开头的方法,但写法有误。正确的函数是: ### ✅ 正确方法名:`str.startswith()` 注意: - 是 **`startswith`**,不是 `startwith` - 它是一个字符串方法,用于检查字符串是否以指定的前缀开始 - 返回布尔值:`True` 或 `False` --- ### ✅ 语法: ```python str.startswith(prefix[, start[, end]]) ``` - `prefix`: 要检查的前缀(可以是字符串,也可以是元组) - `start` (可选): 开始索引 - `end` (可选): 结束索引 --- ### ✅ 示例代码: #### 1. 基本用法(单个前缀) ```python text = "Hello, world!" print(text.startswith("Hello")) # 输出: True print(text.startswith("World")) # 输出: False(区分大小写) ``` #### 2. 使用元组匹配多个可能的前缀 ```python filename = "image.png" print(filename.startswith(("http://", "https://"))) # 检查是否为 URL # 输出: False url = "https://example.com" print(url.startswith(("http://", "https://"))) # 输出: True ``` 这个技巧在判断文件协议、路径类型时非常有用。 #### 3. 指定起始位置 ```python text = "abababc" print(text.startswith("ab", 2)) # 从索引 2 开始检查 # 输出: True(因为 text[2:] == "ababc",确实以 "ab" 开头) ``` --- ### ❌ 常见错误写法 ```python text.startwith("Hello") # ❌ 方法名拼错 text.startswith("hello") # ❌ 小写,原字符串是大写 H,会返回 False text.startswith(123) # ❌ 参数不是字符串或元组,会报 TypeError ``` --- ### 🔍 实际应用场景 #### 场景1:过滤日志中以特定级别开头的行 ```python logs = [ "INFO: User logged in", "ERROR: Database connection failed", "INFO: Data fetched successfully" ] info_logs = [log for log in logs if log.startswith("INFO")] print(info_logs) # 输出: ['INFO: User logged in', 'INFO: Data fetched successfully'] ``` #### 场景2:判断 URL 协议 ```python def is_secure_url(url): return url.startswith("https://") print(is_secure_url("https://google.com")) # True print(is_secure_url("http://google.com")) # False ``` --- ### ✅ 总结 | 特性 | 说明 | |------|------| | 方法名 | `startswith()`(注意是 `starts with` 两个词) | | 返回值 | `True` / `False` | | 是否区分大小写 | 是 | | 支持多前缀 | 可传入元组 | | 安全性 | 不会抛出异常,推荐用于条件判断 | ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值