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;以上语句执行报错Unknown column 't1.cbdl' in where clause
时间: 2024-04-20 08:23:12 浏览: 82
根据错误信息,报错的原因是在 WHERE 子句中找不到 't1.cbdl' 这个列。这可能是由于表 pms_cbdl 中没有名为 'cbdl' 的列造成的。请确保在 WHERE 子句中使用的列名与表 pms_cbdl 中的列名一致。
如果列名是正确的,还请检查表 pms_cbdl 是否存在,并且确保已正确连接到数据库。如果表名和列名都正确,并且仍然出现此错误,请提供完整的表结构和数据,以便我能够更好地帮助您解决问题。谢谢!
相关问题
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;以上语句执行报错
您提供的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的长度进行连接。这样可以避免子查询返回多个结果的问题。
阅读全文