SELECT CASE WHEN EXISTS (SELECT 1 FROM uf_zttsryb WHERE ry='$detail_1.ry$' and n='$detail_1.n$' and y='$detail_1.y$') THEN (SELECT ztts FROM uf_zttsryb WHERE ry='$detail_1.ry$' and n='$detail_1.n$' and y='$detail_1.y$') ELSE '0' END AS ztts FROM uf_zttsryb WHERE ry='$detail_1.ry$' and n='$detail_1.n$' and y='$detail_1.y$' 错在哪里
时间: 2023-07-16 22:14:17 浏览: 84
这是一个SQL语句,其中存在问题。问题在于,在SELECT语句中使用了子查询,但是没有给子查询指定别名,导致语法错误。应该将子查询的结果指定一个别名,例如:
```
SELECT
CASE
WHEN EXISTS (SELECT 1 FROM uf_zttsryb WHERE ry='$detail_1.ry$' and n='$detail_1.n$' and y='$detail_1.y$')
THEN (SELECT ztts FROM uf_zttsryb WHERE ry='$detail_1.ry$' and n='$detail_1.n$' and y='$detail_1.y$')
ELSE '0'
END AS ztts_result
FROM uf_zttsryb
WHERE ry='$detail_1.ry$' and n='$detail_1.n$' and y='$detail_1.y$'
```
这样修改后,子查询的结果可以通过别名"ztts_result"在外层SELECT语句中使用。
阅读全文