我已在SELECT中声明了变量upload_source
,但收到错误消息
“ where子句”中的未知列“ upload_source”
SELECT b.name as name, filename, upload_date, IF(filename LIKE '%.png%', 1, 0) as type, IF(ex_link!='', 0, 1) as upload_source, title, a.id
FROM (`all_media` a)
LEFT JOIN `admins` b ON `b`.`id`=`user_id`
WHERE `a`.`approved` = 0
AND `upload_source` = 0
ORDER BY `filename` DESC
我该如何解决?
答案 0 :(得分:2)
您不能在where子句中使用别名的表达式/列。在Select
子句中使用相同的表达式(在Where
子句中定义)。
可以在查询选择列表中使用别名为列提供一个 不同的名字。您可以在GROUP BY,ORDER BY或HAVING中使用别名 子句引用该列。
标准SQL不允许在WHERE子句中引用列别名。 施加此限制是因为当WHERE子句是 进行评估,则可能尚未确定列值。
请改为执行以下操作(我已将upload_source = 0
简化为ex_link != ''
):
SELECT b.name as name,
filename,
upload_date,
IF(filename LIKE '%.png%', 1, 0) as type,
IF(ex_link!='', 0, 1) as upload_source,
title,
a.id
FROM `all_media` AS a
LEFT JOIN `admins` b ON `b`.`id`=`user_id`
WHERE `a`.`approved` = 0
AND ex_link != ''
ORDER BY `filename` DESC
答案 1 :(得分:1)
在SQL中, WHERE 关键字不能与集合函数一起使用。 尝试使用具有条款:
SELECT b.name as name, filename, upload_date,
IF(filename LIKE '%.png%', 1, 0) as type, IF(ex_link!='', 0, 1) as upload_source,title, a.id
FROM (
all_media a)
LEFT JOIN
管理员b ON
b .
id =
user_id
WHERE
a .
已批准= 0
HAVING
upload_source = 0
ORDER BY
文件名DESC