当我不使用WHERE
子句时,查询工作正常。如何使其与WHERE
子句一起使用?这太雄心勃勃了吗?
由于
错误:
[Err] 1054 - Unknown column 'claims.client_name' in 'where clause'
SQL:
SELECT
(
CASE
WHEN client = 'Driver' THEN (SELECT fullname FROM driver WHERE id = claims.client_id)
WHEN client = 'Passenger' THEN (SELECT fullname FROM passengers WHERE id = claims.client_id)
WHEN client = 'Thirdparty' THEN (SELECT fullname FROM thirdparty WHERE id = claims.client_id)
WHEN client = '' THEN NULL
END
) AS client_name
FROM claims
WHERE (client_name LIKE '%john%')
答案 0 :(得分:2)
替换
WHERE (client_name LIKE '%john%')
与
HAVING (client_name LIKE '%john%')
答案 1 :(得分:0)
试试这个: -
SELECT CASE WHEN client = 'Driver' THEN (SELECT fullname FROM driver WHERE id = claims.client_id)
WHEN client = 'Passenger' THEN (SELECT fullname FROM passengers WHERE id = claims.client_id)
WHEN client = 'Thirdparty' THEN (SELECT fullname FROM thirdparty WHERE id = claims.client_id)
WHEN client = '' THEN NULL
END AS client_name FROM claims WHERE client_name LIKE '%john%'
希望它会对你有所帮助。
答案 2 :(得分:0)
您不能在where子句中使用别名。通常,您会重复给出别名的代码并将其放入。
即,而不是放"WHERE client_name LIKE '%john%'"
你可以尝试:
WHERE (CASE
WHEN client = 'Driver' THEN (SELECT fullname FROM driver WHERE id =
claims.client_id)
WHEN client = 'Passenger' THEN (SELECT fullname FROM passengers WHERE id =
claims.client_id)
WHEN client = 'Thirdparty' THEN (SELECT fullname FROM thirdparty WHERE id =
claims.client_id)
WHEN client = '' THEN NULL
END) LIKE '%john%'
我还没有在开发工具中试过这个,所以它可能需要一些抛光。
答案 3 :(得分:0)
您不能在WHERE子句中直接使用别名。检查答案HERE。遗憾的是,您无法使用SQL Server中可用的CTE,而是可以使用
临时表。
尝试以下声明
SELECT tmp.* FROM
(
SELECT CASE
WHEN client = 'Driver' THEN (SELECT fullname FROM driver WHERE id = claims.client_id)
WHEN client = 'Passenger' THEN (SELECT fullname FROM passengers WHERE id = claims.client_id)
WHEN client = 'Thirdparty' THEN (SELECT fullname FROM thirdparty WHERE id = claims.client_id)
WHEN client = '' THEN NULL
END AS client_name FROM claims
) AS tmp
WHERE tmp.client_name LIKE '%john%'