SELECT cbdl_code, CASE WHEN LENGTH(cbdl_code) = 1 THEN name WHEN LENGTH(cbdl_code) = 3 THEN CONCAT_WS('/', (SELECT name FROM pms_cbdl WHERE cbdl_code = SUBSTRING(t1.cbdl_code, 1, 1)), (SELECT name FROM pms_cbdl WHERE cbdl_code = t1.cbdl_code)) WHEN LENGTH(cbdl_code) = 4 THEN CONCAT_WS('/', (SELECT name FROM pms_cbdl WHERE cbdl_code = SUBSTRING(t1.cbdl_code, 1, 1)), (SELECT name FROM pms_cbdl WHERE cbdl_code = SUBSTRING(t1.cbdl_code, 1, 3)), (SELECT name FROM pms_cbdl WHERE cbdl_code = t1.cbdl_code)) WHEN LENGTH(cbdl_code) = 5 THEN CONCAT_WS('/', (SELECT name FROM pms_cbdl WHERE cbdl_code = SUBSTRING(t1.cbdl_code, 1, 1)), (SELECT name FROM pms_cbdl WHERE cbdl_code = SUBSTRING(t1.cbdl_code, 1, 3)), (SELECT name FROM pms_cbdl WHERE cbdl_code = SUBSTRING(t1.cbdl_code, 1, 4)), (SELECT name FROM pms_cbdl WHERE cbdl_code = t1.cbdl_code)) END AS new_name FROM pms_cbdl t1;以上语句执行报错
时间: 2024-04-10 08:33:01 浏览: 89
您提供的SQL语句中的错误可能是由于子查询返回多个结果导致的。您可以尝试使用JOIN操作来替代子查询,以解决此问题。以下是修改后的SQL语句:
```sql
SELECT t1.cbdl_code,
CASE
WHEN LENGTH(t1.cbdl_code) = 1 THEN t1.name
WHEN LENGTH(t1.cbdl_code) = 3 THEN CONCAT_WS('/', t2.name, t1.name)
WHEN LENGTH(t1.cbdl_code) = 4 THEN CONCAT_WS('/', t2.name, t3.name, t1.name)
WHEN LENGTH(t1.cbdl_code) = 5 THEN CONCAT_WS('/', t2.name, t3.name, t4.name, t1.name)
END AS new_name
FROM pms_cbdl t1
LEFT JOIN pms_cbdl t2 ON t1.cbdl_code = SUBSTRING(t2.cbdl_code, 1, 1)
LEFT JOIN pms_cbdl t3 ON t1.cbdl_code = SUBSTRING(t3.cbdl_code, 1, 3)
LEFT JOIN pms_cbdl t4 ON t1.cbdl_code = SUBSTRING(t4.cbdl_code, 1, 4);
```
请注意,您可能需要根据您的表结构和数据适当调整JOIN条件和连接的表别名。这个修改后的查询将使用JOIN操作来代替子查询,并根据cbdl_code的长度进行连接。这样可以避免子查询返回多个结果的问题。
阅读全文